From ada4daa84ddc548bbdb9d442cccd93feac27b059 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Sat, 6 Jul 2019 03:46:18 +0000 Subject: [PATCH] [IRBuilder] Introduce helpers for and/or of multiple values at once We had versions of this code scattered around, so consolidate into one location. Not strictly NFC since the order of intermediate results may change in some places, but since these operations are associatives, should not change results. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365259 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/IRBuilder.h | 16 ++++++++++++++++ .../Instrumentation/MemorySanitizer.cpp | 6 +++--- lib/Transforms/Scalar/LoopPredication.cpp | 18 ++++-------------- lib/Transforms/Scalar/SimpleLoopUnswitch.cpp | 11 +++-------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/include/llvm/IR/IRBuilder.h b/include/llvm/IR/IRBuilder.h index 9c2ca64c7b0..980f7345a40 100644 --- a/include/llvm/IR/IRBuilder.h +++ b/include/llvm/IR/IRBuilder.h @@ -1214,6 +1214,14 @@ public: return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); } + Value *CreateAnd(ArrayRef Ops) { + assert(!Ops.empty()); + Value *Accum = Ops[0]; + for (unsigned i = 1; i < Ops.size(); i++) + Accum = CreateAnd(Accum, Ops[i]); + return Accum; + } + Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") { if (auto *RC = dyn_cast(RHS)) { if (RC->isNullValue()) @@ -1232,6 +1240,14 @@ public: return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); } + Value *CreateOr(ArrayRef Ops) { + assert(!Ops.empty()); + Value *Accum = Ops[0]; + for (unsigned i = 1; i < Ops.size(); i++) + Accum = CreateOr(Accum, Ops[i]); + return Accum; + } + Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") { if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V; return Insert(BinaryOperator::CreateXor(LHS, RHS), Name); diff --git a/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/lib/Transforms/Instrumentation/MemorySanitizer.cpp index d4d4979c180..b25cbed1bb0 100644 --- a/lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ b/lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -1943,7 +1943,7 @@ struct MemorySanitizerVisitor : public InstVisitor { Value *S1S2 = IRB.CreateAnd(S1, S2); Value *V1S2 = IRB.CreateAnd(V1, S2); Value *S1V2 = IRB.CreateAnd(S1, V2); - setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2))); + setShadow(&I, IRB.CreateOr({S1S2, V1S2, S1V2})); setOriginForNaryOp(I); } @@ -1965,7 +1965,7 @@ struct MemorySanitizerVisitor : public InstVisitor { Value *S1S2 = IRB.CreateAnd(S1, S2); Value *V1S2 = IRB.CreateAnd(V1, S2); Value *S1V2 = IRB.CreateAnd(S1, V2); - setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2))); + setShadow(&I, IRB.CreateOr({S1S2, V1S2, S1V2})); setOriginForNaryOp(I); } @@ -3508,7 +3508,7 @@ struct MemorySanitizerVisitor : public InstVisitor { D = CreateAppToShadowCast(IRB, D); // Result shadow if condition shadow is 1. - Sa1 = IRB.CreateOr(IRB.CreateXor(C, D), IRB.CreateOr(Sc, Sd)); + Sa1 = IRB.CreateOr({IRB.CreateXor(C, D), Sc, Sd}); } Value *Sa = IRB.CreateSelect(Sb, Sa1, Sa0, "_msprop_select"); setShadow(&I, Sa); diff --git a/lib/Transforms/Scalar/LoopPredication.cpp b/lib/Transforms/Scalar/LoopPredication.cpp index ed715d36984..d3cec7568b6 100644 --- a/lib/Transforms/Scalar/LoopPredication.cpp +++ b/lib/Transforms/Scalar/LoopPredication.cpp @@ -793,14 +793,9 @@ bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard, // Emit the new guard condition IRBuilder<> Builder(findInsertPt(Guard, Checks)); - Value *LastCheck = nullptr; - for (auto *Check : Checks) - if (!LastCheck) - LastCheck = Check; - else - LastCheck = Builder.CreateAnd(LastCheck, Check); + Value *AllChecks = Builder.CreateAnd(Checks); auto *OldCond = Guard->getOperand(0); - Guard->setOperand(0, LastCheck); + Guard->setOperand(0, AllChecks); RecursivelyDeleteTriviallyDeadInstructions(OldCond); LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n"); @@ -824,14 +819,9 @@ bool LoopPredication::widenWidenableBranchGuardConditions( // Emit the new guard condition IRBuilder<> Builder(findInsertPt(BI, Checks)); - Value *LastCheck = nullptr; - for (auto *Check : Checks) - if (!LastCheck) - LastCheck = Check; - else - LastCheck = Builder.CreateAnd(LastCheck, Check); + Value *AllChecks = Builder.CreateAnd(Checks); auto *OldCond = BI->getCondition(); - BI->setCondition(LastCheck); + BI->setCondition(AllChecks); assert(isGuardAsWidenableBranch(BI) && "Stopped being a guard after transform?"); RecursivelyDeleteTriviallyDeadInstructions(OldCond); diff --git a/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp index cb782409a73..82e98ec1877 100644 --- a/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ b/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -180,14 +180,9 @@ static void buildPartialUnswitchConditionalBranch(BasicBlock &BB, BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc) { IRBuilder<> IRB(&BB); - Value *Cond = Invariants.front(); - for (Value *Invariant : - make_range(std::next(Invariants.begin()), Invariants.end())) - if (Direction) - Cond = IRB.CreateOr(Cond, Invariant); - else - Cond = IRB.CreateAnd(Cond, Invariant); - + + Value *Cond = Direction ? IRB.CreateOr(Invariants) : + IRB.CreateAnd(Invariants); IRB.CreateCondBr(Cond, Direction ? &UnswitchedSucc : &NormalSucc, Direction ? &NormalSucc : &UnswitchedSucc); } -- 2.40.0