Skip to content

File tree

5 files changed

+9
-283
lines changed

5 files changed

+9
-283
lines changed

llvm/lib/Transforms/InstCombine/InstCombine.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "llvm/Analysis/AssumptionTracker.h"
1515
#include "llvm/Analysis/TargetFolder.h"
1616
#include "llvm/Analysis/ValueTracking.h"
17-
#include "llvm/IR/Dominators.h"
1817
#include "llvm/IR/IRBuilder.h"
1918
#include "llvm/IR/InstVisitor.h"
2019
#include "llvm/IR/IntrinsicInst.h"
@@ -99,7 +98,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
9998
AssumptionTracker *AT;
10099
const DataLayout *DL;
101100
TargetLibraryInfo *TLI;
102-
DominatorTree *DT;
101+
DominatorTree *DT; // not required
103102
bool MadeIRChange;
104103
LibCallSimplifier *Simplifier;
105104
bool MinimizeSize;
@@ -114,8 +113,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
114113
BuilderTy *Builder;
115114

116115
static char ID; // Pass identification, replacement for typeid
117-
InstCombiner()
118-
: FunctionPass(ID), DL(nullptr), DT(nullptr), Builder(nullptr) {
116+
InstCombiner() : FunctionPass(ID), DL(nullptr), Builder(nullptr) {
119117
MinimizeSize = false;
120118
initializeInstCombinerPass(*PassRegistry::getPassRegistry());
121119
}
@@ -244,11 +242,6 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
244242

245243
// visitInstruction - Specify what to return for unhandled instructions...
246244
Instruction *visitInstruction(Instruction &I) { return nullptr; }
247-
bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
248-
const BasicBlock *DB) const;
249-
bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
250-
const ConstantInt *CI1,
251-
const ConstantInt *CI2);
252245

253246
private:
254247
bool ShouldChangeType(Type *From, Type *To) const;

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 2 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,133 +2429,6 @@ static bool swapMayExposeCSEOpportunities(const Value * Op0,
24292429
return GlobalSwapBenefits > 0;
24302430
}
24312431

2432-
/// \brief Check that one use is in the same block as the definition and all
2433-
/// other uses are in blocks dominated by a given block
2434-
///
2435-
/// \param DI Definition
2436-
/// \param UI Use
2437-
/// \param DB Block that must dominate all uses of \p DI outside
2438-
/// the parent block. Note there can be a use of \p DI in \p DB.
2439-
/// \return true when \p UI is the only use of \p DI in the parent block
2440-
/// and all other uses of \p DI are in blocks dominated by \p DB.
2441-
///
2442-
bool InstCombiner::dominatesAllUses(const Instruction *DI,
2443-
const Instruction *UI,
2444-
const BasicBlock *DB) const {
2445-
assert(DI && UI && DI->getParent() == UI->getParent() &&
2446-
"definition and use must be in the same block");
2447-
// DominatorTree available?
2448-
if (!DT)
2449-
return false;
2450-
for (const User *U : DI->users()) {
2451-
auto *Usr = cast<Instruction>(U);
2452-
if (Usr != UI && !DT->dominates(DB, Usr->getParent()))
2453-
return false;
2454-
}
2455-
return true;
2456-
}
2457-
2458-
///
2459-
/// true when the instruction sequence within a block is select-cmp-br.
2460-
///
2461-
static bool isChainSelectCmpBranch(const SelectInst *SI) {
2462-
const BasicBlock *BB = SI->getParent();
2463-
if (!BB)
2464-
return false;
2465-
auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
2466-
if (!BI || BI->getNumSuccessors() != 2)
2467-
return false;
2468-
auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
2469-
if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
2470-
return false;
2471-
// FIXME: Conservatively suppress the optimization when the IC
2472-
// has a parent different from SI (including no parent). Otherwise
2473-
// the assertion in dominatesAllUses() fires and causes a build failure.
2474-
// Make the optimization safe w/o this condition.
2475-
if (SI->getParent() != IC->getParent())
2476-
return false;
2477-
return true;
2478-
}
2479-
2480-
///
2481-
/// \brief True when a select result is replaced by one of its operands
2482-
/// in select-icmp sequence. This will eventually result in the elimination
2483-
/// of the select.
2484-
///
2485-
/// \param SI Select instruction
2486-
/// \param Icmp Compare instruction
2487-
/// \param CI1 'true' when first select operand is equal to RHSC of Icmp
2488-
/// \param CI2 'true' when second select operand is equal to RHSC of Icmp
2489-
///
2490-
/// Notes:
2491-
/// - The replacement is global and requires dominator information
2492-
/// - The caller is responsible for the actual replacement
2493-
///
2494-
/// Example:
2495-
///
2496-
/// entry:
2497-
/// %4 = select i1 %3, %C* %0, %C* null
2498-
/// %5 = icmp eq %C* %4, null
2499-
/// br i1 %5, label %9, label %7
2500-
/// ...
2501-
/// ; <label>:7 ; preds = %entry
2502-
/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
2503-
/// ...
2504-
///
2505-
/// can be transformed to
2506-
///
2507-
/// %5 = icmp eq %C* %0, null
2508-
/// %6 = select i1 %3, i1 %5, i1 true
2509-
/// br i1 %6, label %9, label %7
2510-
/// ...
2511-
/// ; <label>:7 ; preds = %entry
2512-
/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
2513-
///
2514-
/// Similar when the first operand of the select is a constant or/and
2515-
/// the compare is for not equal rather than equal.
2516-
///
2517-
/// FIXME: Currently the function considers equal compares only. It should be
2518-
/// possbile to extend it to not equal compares also.
2519-
///
2520-
bool InstCombiner::replacedSelectWithOperand(SelectInst *SI,
2521-
const ICmpInst *Icmp,
2522-
const ConstantInt *CI1,
2523-
const ConstantInt *CI2) {
2524-
if (isChainSelectCmpBranch(SI) && Icmp->isEquality()) {
2525-
// Code sequence is select - icmp.[eq|ne] - br
2526-
unsigned ReplaceWithOpd = 0;
2527-
if (CI1 && !CI1->isZero())
2528-
// The first constant operand of the select and the RHS of
2529-
// the compare match, so try to substitute
2530-
// the select results with its second operand
2531-
// Example:
2532-
// %4 = select i1 %3, %C* null, %C* %0
2533-
// %5 = icmp eq %C* %4, null
2534-
// ==> could replace select with second operand
2535-
ReplaceWithOpd = 2;
2536-
else if (CI2 && !CI2->isZero())
2537-
// Similar when the second operand of the select is a constant
2538-
// Example:
2539-
// %4 = select i1 %3, %C* %0, %C* null
2540-
// %5 = icmp eq %C* %4, null
2541-
// ==> could replace select with first operand
2542-
ReplaceWithOpd = 1;
2543-
if (ReplaceWithOpd) {
2544-
// Replace select with operand on else path for EQ compares.
2545-
// Replace select with operand on then path for NE compares.
2546-
BasicBlock *Succ =
2547-
Icmp->getPredicate() == ICmpInst::ICMP_EQ
2548-
? SI->getParent()->getTerminator()->getSuccessor(1)
2549-
: SI->getParent()->getTerminator()->getSuccessor(0);
2550-
if (InstCombiner::dominatesAllUses(SI, Icmp, Succ)) {
2551-
SI->replaceAllUsesWith(SI->getOperand(ReplaceWithOpd));
2552-
return true;
2553-
}
2554-
}
2555-
}
2556-
return false;
2557-
}
2558-
25592432
Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
25602433
bool Changed = false;
25612434
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
@@ -3012,21 +2885,8 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
30122885
// fold to a constant (in which case the icmp is replaced with a select
30132886
// which will usually simplify) or this is the only user of the
30142887
// select (in which case we are trading a select+icmp for a simpler
3015-
// select+icmp) or all uses of the select can be replaced based on
3016-
// dominance information ("Global cases").
3017-
bool Transform = false;
3018-
if (Op1 && Op2)
3019-
Transform = true;
3020-
else if (Op1 || Op2) {
3021-
if (LHSI->hasOneUse())
3022-
Transform = true;
3023-
else
3024-
// Global cases
3025-
Transform = replacedSelectWithOperand(
3026-
cast<SelectInst>(LHSI), &I, dyn_cast_or_null<ConstantInt>(Op1),
3027-
dyn_cast_or_null<ConstantInt>(Op2));
3028-
}
3029-
if (Transform) {
2888+
// select+icmp).
2889+
if ((Op1 && Op2) || (LHSI->hasOneUse() && (Op1 || Op2))) {
30302890
if (!Op1)
30312891
Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1),
30322892
RHSC, I.getName());

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,13 @@ INITIALIZE_PASS_BEGIN(InstCombiner, "instcombine",
9090
"Combine redundant instructions", false, false)
9191
INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
9292
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
93-
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
9493
INITIALIZE_PASS_END(InstCombiner, "instcombine",
9594
"Combine redundant instructions", false, false)
9695

9796
void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
9897
AU.setPreservesCFG();
9998
AU.addRequired<AssumptionTracker>();
10099
AU.addRequired<TargetLibraryInfo>();
101-
AU.addRequired<DominatorTreeWrapperPass>();
102-
AU.addPreserved<DominatorTreeWrapperPass>();
103100
}
104101

105102

@@ -2936,9 +2933,12 @@ bool InstCombiner::runOnFunction(Function &F) {
29362933
AT = &getAnalysis<AssumptionTracker>();
29372934
DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
29382935
DL = DLP ? &DLP->getDataLayout() : nullptr;
2939-
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
29402936
TLI = &getAnalysis<TargetLibraryInfo>();
29412937

2938+
DominatorTreeWrapperPass *DTWP =
2939+
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
2940+
DT = DTWP ? &DTWP->getDomTree() : nullptr;
2941+
29422942
// Minimizing size?
29432943
MinimizeSize = F.getAttributes().hasAttribute(AttributeSet::FunctionIndex,
29442944
Attribute::MinSize);

llvm/test/Transforms/InstCombine/pr12338.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
define void @entry() nounwind {
44
entry:
5-
; CHECK: br label %for.cond
65
br label %for.cond
76

87
for.cond:
98
%local = phi <1 x i32> [ <i32 0>, %entry ], [ %phi2, %cond.end47 ]
9+
; CHECK: sub <1 x i32> <i32 92>, %local
1010
%phi3 = sub <1 x i32> zeroinitializer, %local
1111
br label %cond.end
1212

llvm/test/Transforms/InstCombine/select-cmp-br.ll

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)