blob: 60c7fefb15deedd3c3b70d4d64247be13439d114 [file] [log] [blame]
Chris Lattner233f7dc2002-08-12 21:17:25 +00001//===- InstructionCombining.cpp - Combine multiple instructions -----------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner8a2a3112001-12-14 16:52:21 +00009//
10// InstructionCombining - Combine instructions to form fewer, simple
Dan Gohman844731a2008-05-13 00:00:25 +000011// instructions. This pass does not modify the CFG. This pass is where
12// algebraic simplification happens.
Chris Lattner8a2a3112001-12-14 16:52:21 +000013//
14// This pass combines things like:
Chris Lattner318bf792007-03-18 22:51:34 +000015// %Y = add i32 %X, 1
16// %Z = add i32 %Y, 1
Chris Lattner8a2a3112001-12-14 16:52:21 +000017// into:
Chris Lattner318bf792007-03-18 22:51:34 +000018// %Z = add i32 %X, 2
Chris Lattner8a2a3112001-12-14 16:52:21 +000019//
20// This is a simple worklist driven algorithm.
21//
Chris Lattner065a6162003-09-10 05:29:43 +000022// This pass guarantees that the following canonicalizations are performed on
Chris Lattner2cd91962003-07-23 21:41:57 +000023// the program:
24// 1. If a binary operator has a constant operand, it is moved to the RHS
Chris Lattnerdf17af12003-08-12 21:53:41 +000025// 2. Bitwise operators with constant operands are always grouped so that
26// shifts are performed first, then or's, then and's, then xor's.
Reid Spencere4d87aa2006-12-23 06:05:41 +000027// 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
28// 4. All cmp instructions on boolean values are replaced with logical ops
Chris Lattnere92d2f42003-08-13 04:18:28 +000029// 5. add X, X is represented as (X*2) => (X << 1)
30// 6. Multiplies with a power-of-two constant argument are transformed into
31// shifts.
Chris Lattnerbac32862004-11-14 19:13:23 +000032// ... etc.
Chris Lattner2cd91962003-07-23 21:41:57 +000033//
Chris Lattner8a2a3112001-12-14 16:52:21 +000034//===----------------------------------------------------------------------===//
35
Chris Lattner0cea42a2004-03-13 23:54:27 +000036#define DEBUG_TYPE "instcombine"
Chris Lattner022103b2002-05-07 20:03:00 +000037#include "llvm/Transforms/Scalar.h"
Chris Lattnerac8f2fd2010-01-04 07:12:23 +000038#include "InstCombine.h"
Chris Lattner35b9e482004-10-12 04:52:52 +000039#include "llvm/IntrinsicInst.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000040#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner9dbb4292009-11-09 23:28:39 +000041#include "llvm/Analysis/InstructionSimplify.h"
Victor Hernandezf006b182009-10-27 20:05:49 +000042#include "llvm/Analysis/MemoryBuiltins.h"
Chris Lattnerbc61e662003-11-02 05:57:39 +000043#include "llvm/Target/TargetData.h"
Chad Rosier3d925d22011-11-29 23:57:10 +000044#include "llvm/Target/TargetLibraryInfo.h"
Chris Lattnerbc61e662003-11-02 05:57:39 +000045#include "llvm/Transforms/Utils/Local.h"
Chris Lattner804272c2010-01-05 07:54:43 +000046#include "llvm/Support/CFG.h"
Chris Lattnerea1c4542004-12-08 23:43:58 +000047#include "llvm/Support/Debug.h"
Chris Lattner28977af2004-04-05 01:30:19 +000048#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattneracd1f0f2004-07-30 07:50:03 +000049#include "llvm/Support/PatternMatch.h"
Nick Lewyckyd5061a92011-08-03 00:43:35 +000050#include "llvm/Support/ValueHandle.h"
Chris Lattner1f87a582007-02-15 19:41:52 +000051#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000052#include "llvm/ADT/Statistic.h"
Duncan Sands0ad7b6e2011-09-30 13:12:16 +000053#include "llvm/ADT/StringSwitch.h"
Owen Anderson74cfb0c2010-10-07 20:04:55 +000054#include "llvm-c/Initialization.h"
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +000055#include <algorithm>
Torok Edwin3eaee312008-04-20 08:33:11 +000056#include <climits>
Chris Lattner67b1e1b2003-12-07 01:24:23 +000057using namespace llvm;
Chris Lattneracd1f0f2004-07-30 07:50:03 +000058using namespace llvm::PatternMatch;
Brian Gaeked0fde302003-11-11 22:41:34 +000059
Chris Lattner0e5f4992006-12-19 21:40:18 +000060STATISTIC(NumCombined , "Number of insts combined");
61STATISTIC(NumConstProp, "Number of constant folds");
62STATISTIC(NumDeadInst , "Number of dead inst eliminated");
Chris Lattner0e5f4992006-12-19 21:40:18 +000063STATISTIC(NumSunkInst , "Number of instructions sunk");
Duncan Sands37bf92b2010-12-22 13:36:08 +000064STATISTIC(NumExpand, "Number of expansions");
Duncan Sandsa3c44a52010-12-22 09:40:51 +000065STATISTIC(NumFactor , "Number of factorizations");
66STATISTIC(NumReassoc , "Number of reassociations");
Chris Lattnera92f6962002-10-01 22:38:41 +000067
Owen Anderson74cfb0c2010-10-07 20:04:55 +000068// Initialization Routines
69void llvm::initializeInstCombine(PassRegistry &Registry) {
70 initializeInstCombinerPass(Registry);
71}
72
73void LLVMInitializeInstCombine(LLVMPassRegistryRef R) {
74 initializeInstCombine(*unwrap(R));
75}
Chris Lattnerdd841ae2002-04-18 17:39:14 +000076
Dan Gohman844731a2008-05-13 00:00:25 +000077char InstCombiner::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000078INITIALIZE_PASS(InstCombiner, "instcombine",
Owen Andersonce665bd2010-10-07 22:25:06 +000079 "Combine redundant instructions", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000080
Chris Lattnere0b4b722010-01-04 07:17:19 +000081void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnere0b4b722010-01-04 07:17:19 +000082 AU.setPreservesCFG();
Chad Rosier3d925d22011-11-29 23:57:10 +000083 AU.addRequired<TargetLibraryInfo>();
Chris Lattnere0b4b722010-01-04 07:17:19 +000084}
85
86
Chris Lattnerc22d4d12009-11-10 07:23:37 +000087/// ShouldChangeType - Return true if it is desirable to convert a computation
88/// from 'From' to 'To'. We don't want to convert from a legal to an illegal
89/// type for example, or from a smaller to a larger illegal type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000090bool InstCombiner::ShouldChangeType(Type *From, Type *To) const {
Duncan Sands1df98592010-02-16 11:11:14 +000091 assert(From->isIntegerTy() && To->isIntegerTy());
Chris Lattnerc22d4d12009-11-10 07:23:37 +000092
93 // If we don't have TD, we don't know if the source/dest are legal.
94 if (!TD) return false;
95
96 unsigned FromWidth = From->getPrimitiveSizeInBits();
97 unsigned ToWidth = To->getPrimitiveSizeInBits();
98 bool FromLegal = TD->isLegalInteger(FromWidth);
99 bool ToLegal = TD->isLegalInteger(ToWidth);
100
101 // If this is a legal integer from type, and the result would be an illegal
102 // type, don't do the transformation.
103 if (FromLegal && !ToLegal)
104 return false;
105
106 // Otherwise, if both are illegal, do not increase the size of the result. We
107 // do allow things like i160 -> i64, but not i64 -> i160.
108 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
109 return false;
110
111 return true;
112}
113
Nick Lewyckydaf27ea2011-08-14 01:45:19 +0000114// Return true, if No Signed Wrap should be maintained for I.
115// The No Signed Wrap flag can be kept if the operation "B (I.getOpcode) C",
116// where both B and C should be ConstantInts, results in a constant that does
117// not overflow. This function only handles the Add and Sub opcodes. For
118// all other opcodes, the function conservatively returns false.
119static bool MaintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C) {
120 OverflowingBinaryOperator *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
121 if (!OBO || !OBO->hasNoSignedWrap()) {
122 return false;
123 }
124
125 // We reason about Add and Sub Only.
126 Instruction::BinaryOps Opcode = I.getOpcode();
127 if (Opcode != Instruction::Add &&
128 Opcode != Instruction::Sub) {
129 return false;
130 }
131
132 ConstantInt *CB = dyn_cast<ConstantInt>(B);
133 ConstantInt *CC = dyn_cast<ConstantInt>(C);
134
135 if (!CB || !CC) {
136 return false;
137 }
138
139 const APInt &BVal = CB->getValue();
140 const APInt &CVal = CC->getValue();
141 bool Overflow = false;
142
143 if (Opcode == Instruction::Add) {
144 BVal.sadd_ov(CVal, Overflow);
145 } else {
146 BVal.ssub_ov(CVal, Overflow);
147 }
148
149 return !Overflow;
150}
151
Duncan Sands096aa792010-11-13 15:10:37 +0000152/// SimplifyAssociativeOrCommutative - This performs a few simplifications for
153/// operators which are associative or commutative:
154//
155// Commutative operators:
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000156//
Chris Lattner4f98c562003-03-10 21:43:22 +0000157// 1. Order operands such that they are listed from right (least complex) to
158// left (most complex). This puts constants before unary operators before
159// binary operators.
160//
Duncan Sands096aa792010-11-13 15:10:37 +0000161// Associative operators:
Chris Lattner4f98c562003-03-10 21:43:22 +0000162//
Duncan Sands096aa792010-11-13 15:10:37 +0000163// 2. Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
164// 3. Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
165//
166// Associative and commutative operators:
167//
168// 4. Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
169// 5. Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
170// 6. Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
171// if C1 and C2 are constants.
172//
173bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
Chris Lattner4f98c562003-03-10 21:43:22 +0000174 Instruction::BinaryOps Opcode = I.getOpcode();
Duncan Sands096aa792010-11-13 15:10:37 +0000175 bool Changed = false;
Chris Lattnerc8802d22003-03-11 00:12:48 +0000176
Duncan Sands096aa792010-11-13 15:10:37 +0000177 do {
178 // Order operands such that they are listed from right (least complex) to
179 // left (most complex). This puts constants before unary operators before
180 // binary operators.
181 if (I.isCommutative() && getComplexity(I.getOperand(0)) <
182 getComplexity(I.getOperand(1)))
183 Changed = !I.swapOperands();
184
185 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(I.getOperand(0));
186 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(I.getOperand(1));
187
188 if (I.isAssociative()) {
189 // Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
190 if (Op0 && Op0->getOpcode() == Opcode) {
191 Value *A = Op0->getOperand(0);
192 Value *B = Op0->getOperand(1);
193 Value *C = I.getOperand(1);
194
195 // Does "B op C" simplify?
196 if (Value *V = SimplifyBinOp(Opcode, B, C, TD)) {
197 // It simplifies to V. Form "A op V".
198 I.setOperand(0, A);
199 I.setOperand(1, V);
Dan Gohman5195b712011-02-02 02:05:46 +0000200 // Conservatively clear the optional flags, since they may not be
201 // preserved by the reassociation.
Nick Lewycky7f0170c2011-08-14 03:41:33 +0000202 if (MaintainNoSignedWrap(I, B, C) &&
203 (!Op0 || (isa<BinaryOperator>(Op0) && Op0->hasNoSignedWrap()))) {
204 // Note: this is only valid because SimplifyBinOp doesn't look at
205 // the operands to Op0.
Nick Lewyckydaf27ea2011-08-14 01:45:19 +0000206 I.clearSubclassOptionalData();
207 I.setHasNoSignedWrap(true);
208 } else {
209 I.clearSubclassOptionalData();
210 }
211
Duncan Sands096aa792010-11-13 15:10:37 +0000212 Changed = true;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000213 ++NumReassoc;
Duncan Sands096aa792010-11-13 15:10:37 +0000214 continue;
Misha Brukmanfd939082005-04-21 23:48:37 +0000215 }
Duncan Sands096aa792010-11-13 15:10:37 +0000216 }
217
218 // Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
219 if (Op1 && Op1->getOpcode() == Opcode) {
220 Value *A = I.getOperand(0);
221 Value *B = Op1->getOperand(0);
222 Value *C = Op1->getOperand(1);
223
224 // Does "A op B" simplify?
225 if (Value *V = SimplifyBinOp(Opcode, A, B, TD)) {
226 // It simplifies to V. Form "V op C".
227 I.setOperand(0, V);
228 I.setOperand(1, C);
Dan Gohman5195b712011-02-02 02:05:46 +0000229 // Conservatively clear the optional flags, since they may not be
230 // preserved by the reassociation.
231 I.clearSubclassOptionalData();
Duncan Sands096aa792010-11-13 15:10:37 +0000232 Changed = true;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000233 ++NumReassoc;
Duncan Sands096aa792010-11-13 15:10:37 +0000234 continue;
235 }
236 }
Chris Lattner4f98c562003-03-10 21:43:22 +0000237 }
Duncan Sands096aa792010-11-13 15:10:37 +0000238
239 if (I.isAssociative() && I.isCommutative()) {
240 // Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
241 if (Op0 && Op0->getOpcode() == Opcode) {
242 Value *A = Op0->getOperand(0);
243 Value *B = Op0->getOperand(1);
244 Value *C = I.getOperand(1);
245
246 // Does "C op A" simplify?
247 if (Value *V = SimplifyBinOp(Opcode, C, A, TD)) {
248 // It simplifies to V. Form "V op B".
249 I.setOperand(0, V);
250 I.setOperand(1, B);
Dan Gohman5195b712011-02-02 02:05:46 +0000251 // Conservatively clear the optional flags, since they may not be
252 // preserved by the reassociation.
253 I.clearSubclassOptionalData();
Duncan Sands096aa792010-11-13 15:10:37 +0000254 Changed = true;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000255 ++NumReassoc;
Duncan Sands096aa792010-11-13 15:10:37 +0000256 continue;
257 }
258 }
259
260 // Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
261 if (Op1 && Op1->getOpcode() == Opcode) {
262 Value *A = I.getOperand(0);
263 Value *B = Op1->getOperand(0);
264 Value *C = Op1->getOperand(1);
265
266 // Does "C op A" simplify?
267 if (Value *V = SimplifyBinOp(Opcode, C, A, TD)) {
268 // It simplifies to V. Form "B op V".
269 I.setOperand(0, B);
270 I.setOperand(1, V);
Dan Gohman5195b712011-02-02 02:05:46 +0000271 // Conservatively clear the optional flags, since they may not be
272 // preserved by the reassociation.
273 I.clearSubclassOptionalData();
Duncan Sands096aa792010-11-13 15:10:37 +0000274 Changed = true;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000275 ++NumReassoc;
Duncan Sands096aa792010-11-13 15:10:37 +0000276 continue;
277 }
278 }
279
280 // Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
281 // if C1 and C2 are constants.
282 if (Op0 && Op1 &&
283 Op0->getOpcode() == Opcode && Op1->getOpcode() == Opcode &&
284 isa<Constant>(Op0->getOperand(1)) &&
285 isa<Constant>(Op1->getOperand(1)) &&
286 Op0->hasOneUse() && Op1->hasOneUse()) {
287 Value *A = Op0->getOperand(0);
288 Constant *C1 = cast<Constant>(Op0->getOperand(1));
289 Value *B = Op1->getOperand(0);
290 Constant *C2 = cast<Constant>(Op1->getOperand(1));
291
292 Constant *Folded = ConstantExpr::get(Opcode, C1, C2);
Nick Lewyckydaf27ea2011-08-14 01:45:19 +0000293 BinaryOperator *New = BinaryOperator::Create(Opcode, A, B);
Eli Friedmana311c342011-05-27 00:19:40 +0000294 InsertNewInstWith(New, I);
Eli Friedmane6f364b2011-05-18 23:58:37 +0000295 New->takeName(Op1);
Duncan Sands096aa792010-11-13 15:10:37 +0000296 I.setOperand(0, New);
297 I.setOperand(1, Folded);
Dan Gohman5195b712011-02-02 02:05:46 +0000298 // Conservatively clear the optional flags, since they may not be
299 // preserved by the reassociation.
Nick Lewycky28b84ff2011-08-14 04:51:49 +0000300 I.clearSubclassOptionalData();
Nick Lewyckydaf27ea2011-08-14 01:45:19 +0000301
Duncan Sands096aa792010-11-13 15:10:37 +0000302 Changed = true;
303 continue;
304 }
305 }
306
307 // No further simplifications.
308 return Changed;
309 } while (1);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000310}
Chris Lattner8a2a3112001-12-14 16:52:21 +0000311
Duncan Sands5057f382010-11-23 14:23:47 +0000312/// LeftDistributesOverRight - Whether "X LOp (Y ROp Z)" is always equal to
Duncan Sandsc2b1c0b2010-11-23 15:25:34 +0000313/// "(X LOp Y) ROp (X LOp Z)".
Duncan Sands5057f382010-11-23 14:23:47 +0000314static bool LeftDistributesOverRight(Instruction::BinaryOps LOp,
315 Instruction::BinaryOps ROp) {
316 switch (LOp) {
317 default:
318 return false;
319
320 case Instruction::And:
321 // And distributes over Or and Xor.
322 switch (ROp) {
323 default:
324 return false;
325 case Instruction::Or:
326 case Instruction::Xor:
327 return true;
328 }
329
330 case Instruction::Mul:
331 // Multiplication distributes over addition and subtraction.
332 switch (ROp) {
333 default:
334 return false;
335 case Instruction::Add:
336 case Instruction::Sub:
337 return true;
338 }
339
340 case Instruction::Or:
341 // Or distributes over And.
342 switch (ROp) {
343 default:
344 return false;
345 case Instruction::And:
346 return true;
347 }
348 }
349}
350
351/// RightDistributesOverLeft - Whether "(X LOp Y) ROp Z" is always equal to
352/// "(X ROp Z) LOp (Y ROp Z)".
353static bool RightDistributesOverLeft(Instruction::BinaryOps LOp,
354 Instruction::BinaryOps ROp) {
355 if (Instruction::isCommutative(ROp))
356 return LeftDistributesOverRight(ROp, LOp);
357 // TODO: It would be nice to handle division, aka "(X + Y)/Z = X/Z + Y/Z",
358 // but this requires knowing that the addition does not overflow and other
359 // such subtleties.
360 return false;
361}
362
Duncan Sands37bf92b2010-12-22 13:36:08 +0000363/// SimplifyUsingDistributiveLaws - This tries to simplify binary operations
364/// which some other binary operation distributes over either by factorizing
365/// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this
366/// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is
367/// a win). Returns the simplified value, or null if it didn't simplify.
368Value *InstCombiner::SimplifyUsingDistributiveLaws(BinaryOperator &I) {
369 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
370 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
371 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
372 Instruction::BinaryOps TopLevelOpcode = I.getOpcode(); // op
Duncan Sands5057f382010-11-23 14:23:47 +0000373
Duncan Sands37bf92b2010-12-22 13:36:08 +0000374 // Factorization.
375 if (Op0 && Op1 && Op0->getOpcode() == Op1->getOpcode()) {
376 // The instruction has the form "(A op' B) op (C op' D)". Try to factorize
377 // a common term.
378 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1);
379 Value *C = Op1->getOperand(0), *D = Op1->getOperand(1);
380 Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op'
Duncan Sands5057f382010-11-23 14:23:47 +0000381
Duncan Sands37bf92b2010-12-22 13:36:08 +0000382 // Does "X op' Y" always equal "Y op' X"?
383 bool InnerCommutative = Instruction::isCommutative(InnerOpcode);
Duncan Sands5057f382010-11-23 14:23:47 +0000384
Duncan Sands37bf92b2010-12-22 13:36:08 +0000385 // Does "X op' (Y op Z)" always equal "(X op' Y) op (X op' Z)"?
386 if (LeftDistributesOverRight(InnerOpcode, TopLevelOpcode))
387 // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
388 // commutative case, "(A op' B) op (C op' A)"?
389 if (A == C || (InnerCommutative && A == D)) {
390 if (A != C)
391 std::swap(C, D);
392 // Consider forming "A op' (B op D)".
393 // If "B op D" simplifies then it can be formed with no cost.
394 Value *V = SimplifyBinOp(TopLevelOpcode, B, D, TD);
395 // If "B op D" doesn't simplify then only go on if both of the existing
396 // operations "A op' B" and "C op' D" will be zapped as no longer used.
397 if (!V && Op0->hasOneUse() && Op1->hasOneUse())
398 V = Builder->CreateBinOp(TopLevelOpcode, B, D, Op1->getName());
399 if (V) {
400 ++NumFactor;
401 V = Builder->CreateBinOp(InnerOpcode, A, V);
402 V->takeName(&I);
403 return V;
404 }
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000405 }
Duncan Sands5057f382010-11-23 14:23:47 +0000406
Duncan Sands37bf92b2010-12-22 13:36:08 +0000407 // Does "(X op Y) op' Z" always equal "(X op' Z) op (Y op' Z)"?
408 if (RightDistributesOverLeft(TopLevelOpcode, InnerOpcode))
409 // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
410 // commutative case, "(A op' B) op (B op' D)"?
411 if (B == D || (InnerCommutative && B == C)) {
412 if (B != D)
413 std::swap(C, D);
414 // Consider forming "(A op C) op' B".
415 // If "A op C" simplifies then it can be formed with no cost.
416 Value *V = SimplifyBinOp(TopLevelOpcode, A, C, TD);
417 // If "A op C" doesn't simplify then only go on if both of the existing
418 // operations "A op' B" and "C op' D" will be zapped as no longer used.
419 if (!V && Op0->hasOneUse() && Op1->hasOneUse())
420 V = Builder->CreateBinOp(TopLevelOpcode, A, C, Op0->getName());
421 if (V) {
422 ++NumFactor;
423 V = Builder->CreateBinOp(InnerOpcode, V, B);
424 V->takeName(&I);
425 return V;
426 }
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000427 }
Duncan Sands37bf92b2010-12-22 13:36:08 +0000428 }
429
430 // Expansion.
431 if (Op0 && RightDistributesOverLeft(Op0->getOpcode(), TopLevelOpcode)) {
432 // The instruction has the form "(A op' B) op C". See if expanding it out
433 // to "(A op C) op' (B op C)" results in simplifications.
434 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
435 Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op'
436
437 // Do "A op C" and "B op C" both simplify?
438 if (Value *L = SimplifyBinOp(TopLevelOpcode, A, C, TD))
439 if (Value *R = SimplifyBinOp(TopLevelOpcode, B, C, TD)) {
440 // They do! Return "L op' R".
441 ++NumExpand;
442 // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
443 if ((L == A && R == B) ||
444 (Instruction::isCommutative(InnerOpcode) && L == B && R == A))
445 return Op0;
446 // Otherwise return "L op' R" if it simplifies.
447 if (Value *V = SimplifyBinOp(InnerOpcode, L, R, TD))
448 return V;
449 // Otherwise, create a new instruction.
450 C = Builder->CreateBinOp(InnerOpcode, L, R);
451 C->takeName(&I);
452 return C;
453 }
454 }
455
456 if (Op1 && LeftDistributesOverRight(TopLevelOpcode, Op1->getOpcode())) {
457 // The instruction has the form "A op (B op' C)". See if expanding it out
458 // to "(A op B) op' (A op C)" results in simplifications.
459 Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
460 Instruction::BinaryOps InnerOpcode = Op1->getOpcode(); // op'
461
462 // Do "A op B" and "A op C" both simplify?
463 if (Value *L = SimplifyBinOp(TopLevelOpcode, A, B, TD))
464 if (Value *R = SimplifyBinOp(TopLevelOpcode, A, C, TD)) {
465 // They do! Return "L op' R".
466 ++NumExpand;
467 // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
468 if ((L == B && R == C) ||
469 (Instruction::isCommutative(InnerOpcode) && L == C && R == B))
470 return Op1;
471 // Otherwise return "L op' R" if it simplifies.
472 if (Value *V = SimplifyBinOp(InnerOpcode, L, R, TD))
473 return V;
474 // Otherwise, create a new instruction.
475 A = Builder->CreateBinOp(InnerOpcode, L, R);
476 A->takeName(&I);
477 return A;
478 }
479 }
Duncan Sands5057f382010-11-23 14:23:47 +0000480
481 return 0;
482}
483
Chris Lattner8d969642003-03-10 23:06:50 +0000484// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
485// if the LHS is a constant zero (which is the 'negate' form).
Chris Lattnerb35dde12002-05-06 16:49:18 +0000486//
Chris Lattner02446fc2010-01-04 07:37:31 +0000487Value *InstCombiner::dyn_castNegVal(Value *V) const {
Owen Andersonfa82b6e2009-07-13 22:18:28 +0000488 if (BinaryOperator::isNeg(V))
Chris Lattnera1df33c2005-04-24 07:30:14 +0000489 return BinaryOperator::getNegArgument(V);
Chris Lattner8d969642003-03-10 23:06:50 +0000490
Chris Lattner0ce85802004-12-14 20:08:06 +0000491 // Constants can be considered to be negated values if they can be folded.
492 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
Owen Andersonbaf3c402009-07-29 18:55:55 +0000493 return ConstantExpr::getNeg(C);
Nick Lewycky18b3da62008-05-23 04:54:45 +0000494
495 if (ConstantVector *C = dyn_cast<ConstantVector>(V))
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000496 if (C->getType()->getElementType()->isIntegerTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000497 return ConstantExpr::getNeg(C);
Nick Lewycky18b3da62008-05-23 04:54:45 +0000498
Chris Lattner8d969642003-03-10 23:06:50 +0000499 return 0;
Chris Lattnerb35dde12002-05-06 16:49:18 +0000500}
501
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000502// dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the
503// instruction if the LHS is a constant negative zero (which is the 'negate'
504// form).
505//
Chris Lattnerd12c27c2010-01-05 06:09:35 +0000506Value *InstCombiner::dyn_castFNegVal(Value *V) const {
Owen Andersonfa82b6e2009-07-13 22:18:28 +0000507 if (BinaryOperator::isFNeg(V))
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000508 return BinaryOperator::getFNegArgument(V);
509
510 // Constants can be considered to be negated values if they can be folded.
511 if (ConstantFP *C = dyn_cast<ConstantFP>(V))
Owen Andersonbaf3c402009-07-29 18:55:55 +0000512 return ConstantExpr::getFNeg(C);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000513
514 if (ConstantVector *C = dyn_cast<ConstantVector>(V))
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000515 if (C->getType()->getElementType()->isFloatingPointTy())
Owen Andersonbaf3c402009-07-29 18:55:55 +0000516 return ConstantExpr::getFNeg(C);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000517
518 return 0;
519}
520
Chris Lattner6e7ba452005-01-01 16:22:27 +0000521static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
Chris Lattner2eefe512004-04-09 19:05:30 +0000522 InstCombiner *IC) {
Nick Lewyckyacf4a7c2011-01-21 02:30:43 +0000523 if (CastInst *CI = dyn_cast<CastInst>(&I)) {
Chris Lattner2345d1d2009-08-30 20:01:10 +0000524 return IC->Builder->CreateCast(CI->getOpcode(), SO, I.getType());
Nick Lewyckyacf4a7c2011-01-21 02:30:43 +0000525 }
Chris Lattner6e7ba452005-01-01 16:22:27 +0000526
Chris Lattner2eefe512004-04-09 19:05:30 +0000527 // Figure out if the constant is the left or the right argument.
Chris Lattner6e7ba452005-01-01 16:22:27 +0000528 bool ConstIsRHS = isa<Constant>(I.getOperand(1));
529 Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
Chris Lattner564a7272003-08-13 19:01:45 +0000530
Chris Lattner2eefe512004-04-09 19:05:30 +0000531 if (Constant *SOC = dyn_cast<Constant>(SO)) {
532 if (ConstIsRHS)
Owen Andersonbaf3c402009-07-29 18:55:55 +0000533 return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
534 return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
Chris Lattner2eefe512004-04-09 19:05:30 +0000535 }
536
537 Value *Op0 = SO, *Op1 = ConstOperand;
538 if (!ConstIsRHS)
539 std::swap(Op0, Op1);
Chris Lattner74381062009-08-30 07:44:24 +0000540
Chris Lattner6e7ba452005-01-01 16:22:27 +0000541 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
Chris Lattner74381062009-08-30 07:44:24 +0000542 return IC->Builder->CreateBinOp(BO->getOpcode(), Op0, Op1,
543 SO->getName()+".op");
544 if (ICmpInst *CI = dyn_cast<ICmpInst>(&I))
545 return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
546 SO->getName()+".cmp");
547 if (FCmpInst *CI = dyn_cast<FCmpInst>(&I))
548 return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
549 SO->getName()+".cmp");
550 llvm_unreachable("Unknown binary instruction type!");
Chris Lattner6e7ba452005-01-01 16:22:27 +0000551}
552
553// FoldOpIntoSelect - Given an instruction with a select as one operand and a
554// constant as the other operand, try to fold the binary operator into the
555// select arguments. This also works for Cast instructions, which obviously do
556// not have a second operand.
Chris Lattner80f43d32010-01-04 07:53:58 +0000557Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) {
Chris Lattner6e7ba452005-01-01 16:22:27 +0000558 // Don't modify shared select instructions
559 if (!SI->hasOneUse()) return 0;
560 Value *TV = SI->getOperand(1);
561 Value *FV = SI->getOperand(2);
562
563 if (isa<Constant>(TV) || isa<Constant>(FV)) {
Chris Lattner956db272005-04-21 05:43:13 +0000564 // Bool selects with constant operands can be folded to logical ops.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000565 if (SI->getType()->isIntegerTy(1)) return 0;
Chris Lattner956db272005-04-21 05:43:13 +0000566
Nick Lewyckyacf4a7c2011-01-21 02:30:43 +0000567 // If it's a bitcast involving vectors, make sure it has the same number of
568 // elements on both sides.
569 if (BitCastInst *BC = dyn_cast<BitCastInst>(&Op)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000570 VectorType *DestTy = dyn_cast<VectorType>(BC->getDestTy());
571 VectorType *SrcTy = dyn_cast<VectorType>(BC->getSrcTy());
Nick Lewyckyacf4a7c2011-01-21 02:30:43 +0000572
573 // Verify that either both or neither are vectors.
574 if ((SrcTy == NULL) != (DestTy == NULL)) return 0;
575 // If vectors, verify that they have the same number of elements.
576 if (SrcTy && SrcTy->getNumElements() != DestTy->getNumElements())
577 return 0;
578 }
579
Chris Lattner80f43d32010-01-04 07:53:58 +0000580 Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this);
581 Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this);
Chris Lattner6e7ba452005-01-01 16:22:27 +0000582
Nick Lewyckyacf4a7c2011-01-21 02:30:43 +0000583 return SelectInst::Create(SI->getCondition(),
584 SelectTrueVal, SelectFalseVal);
Chris Lattner6e7ba452005-01-01 16:22:27 +0000585 }
586 return 0;
Chris Lattner2eefe512004-04-09 19:05:30 +0000587}
588
Chris Lattner4e998b22004-09-29 05:07:12 +0000589
Chris Lattner5d1704d2009-09-27 19:57:57 +0000590/// FoldOpIntoPhi - Given a binary operator, cast instruction, or select which
591/// has a PHI node as operand #0, see if we can fold the instruction into the
592/// PHI (which is only possible if all operands to the PHI are constants).
Chris Lattner213cd612009-09-27 20:46:36 +0000593///
Chris Lattner9922ccf2011-01-16 05:14:26 +0000594Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
Chris Lattner4e998b22004-09-29 05:07:12 +0000595 PHINode *PN = cast<PHINode>(I.getOperand(0));
Chris Lattnerbac32862004-11-14 19:13:23 +0000596 unsigned NumPHIValues = PN->getNumIncomingValues();
Chris Lattner5aac8322011-01-16 04:37:29 +0000597 if (NumPHIValues == 0)
Chris Lattner213cd612009-09-27 20:46:36 +0000598 return 0;
599
Chris Lattner084fe622011-01-21 05:08:26 +0000600 // We normally only transform phis with a single use. However, if a PHI has
601 // multiple uses and they are all the same operation, we can fold *all* of the
602 // uses into the PHI.
Chris Lattner192228e2011-01-16 05:28:59 +0000603 if (!PN->hasOneUse()) {
604 // Walk the use list for the instruction, comparing them to I.
605 for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
Chris Lattnercd151d22011-01-21 05:29:50 +0000606 UI != E; ++UI) {
607 Instruction *User = cast<Instruction>(*UI);
608 if (User != &I && !I.isIdenticalTo(User))
Chris Lattner192228e2011-01-16 05:28:59 +0000609 return 0;
Chris Lattnercd151d22011-01-21 05:29:50 +0000610 }
Chris Lattner192228e2011-01-16 05:28:59 +0000611 // Otherwise, we can replace *all* users with the new PHI we form.
612 }
Chris Lattner213cd612009-09-27 20:46:36 +0000613
Chris Lattner5d1704d2009-09-27 19:57:57 +0000614 // Check to see if all of the operands of the PHI are simple constants
615 // (constantint/constantfp/undef). If there is one non-constant value,
Chris Lattnerc6df8f42009-09-27 20:18:49 +0000616 // remember the BB it is in. If there is more than one or if *it* is a PHI,
617 // bail out. We don't do arbitrary constant expressions here because moving
618 // their computation can be expensive without a cost model.
Chris Lattner2a86f3b2006-09-09 22:02:56 +0000619 BasicBlock *NonConstBB = 0;
Chris Lattner5aac8322011-01-16 04:37:29 +0000620 for (unsigned i = 0; i != NumPHIValues; ++i) {
621 Value *InVal = PN->getIncomingValue(i);
622 if (isa<Constant>(InVal) && !isa<ConstantExpr>(InVal))
623 continue;
624
625 if (isa<PHINode>(InVal)) return 0; // Itself a phi.
626 if (NonConstBB) return 0; // More than one non-const value.
627
628 NonConstBB = PN->getIncomingBlock(i);
Chris Lattner7dfe8fd2011-01-16 05:08:00 +0000629
630 // If the InVal is an invoke at the end of the pred block, then we can't
631 // insert a computation after it without breaking the edge.
632 if (InvokeInst *II = dyn_cast<InvokeInst>(InVal))
633 if (II->getParent() == NonConstBB)
634 return 0;
Chris Lattnercd151d22011-01-21 05:29:50 +0000635
636 // If the incoming non-constant value is in I's block, we will remove one
637 // instruction, but insert another equivalent one, leading to infinite
638 // instcombine.
639 if (NonConstBB == I.getParent())
640 return 0;
Chris Lattner5aac8322011-01-16 04:37:29 +0000641 }
Chris Lattner2a86f3b2006-09-09 22:02:56 +0000642
643 // If there is exactly one non-constant value, we can insert a copy of the
644 // operation in that block. However, if this is a critical edge, we would be
645 // inserting the computation one some other paths (e.g. inside a loop). Only
646 // do this if the pred block is unconditionally branching into the phi block.
Chris Lattner9922ccf2011-01-16 05:14:26 +0000647 if (NonConstBB != 0) {
Chris Lattner2a86f3b2006-09-09 22:02:56 +0000648 BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
649 if (!BI || !BI->isUnconditional()) return 0;
650 }
Chris Lattner4e998b22004-09-29 05:07:12 +0000651
652 // Okay, we can do the transformation: create the new PHI node.
Eli Friedmane6f364b2011-05-18 23:58:37 +0000653 PHINode *NewPN = PHINode::Create(I.getType(), PN->getNumIncomingValues());
Chris Lattner857eb572009-10-21 23:41:58 +0000654 InsertNewInstBefore(NewPN, *PN);
655 NewPN->takeName(PN);
Chris Lattner7dfe8fd2011-01-16 05:08:00 +0000656
657 // If we are going to have to insert a new computation, do so right before the
658 // predecessors terminator.
659 if (NonConstBB)
660 Builder->SetInsertPoint(NonConstBB->getTerminator());
661
Chris Lattner4e998b22004-09-29 05:07:12 +0000662 // Next, add all of the operands to the PHI.
Chris Lattner5d1704d2009-09-27 19:57:57 +0000663 if (SelectInst *SI = dyn_cast<SelectInst>(&I)) {
664 // We only currently try to fold the condition of a select when it is a phi,
665 // not the true/false values.
Chris Lattnerc6df8f42009-09-27 20:18:49 +0000666 Value *TrueV = SI->getTrueValue();
667 Value *FalseV = SI->getFalseValue();
Chris Lattner3ddfb212009-09-28 06:49:44 +0000668 BasicBlock *PhiTransBB = PN->getParent();
Chris Lattner5d1704d2009-09-27 19:57:57 +0000669 for (unsigned i = 0; i != NumPHIValues; ++i) {
Chris Lattnerc6df8f42009-09-27 20:18:49 +0000670 BasicBlock *ThisBB = PN->getIncomingBlock(i);
Chris Lattner3ddfb212009-09-28 06:49:44 +0000671 Value *TrueVInPred = TrueV->DoPHITranslation(PhiTransBB, ThisBB);
672 Value *FalseVInPred = FalseV->DoPHITranslation(PhiTransBB, ThisBB);
Chris Lattner5d1704d2009-09-27 19:57:57 +0000673 Value *InV = 0;
Chris Lattner7dfe8fd2011-01-16 05:08:00 +0000674 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
Chris Lattnerc6df8f42009-09-27 20:18:49 +0000675 InV = InC->isNullValue() ? FalseVInPred : TrueVInPred;
Chris Lattner7dfe8fd2011-01-16 05:08:00 +0000676 else
677 InV = Builder->CreateSelect(PN->getIncomingValue(i),
678 TrueVInPred, FalseVInPred, "phitmp");
Chris Lattnerc6df8f42009-09-27 20:18:49 +0000679 NewPN->addIncoming(InV, ThisBB);
Chris Lattner5d1704d2009-09-27 19:57:57 +0000680 }
Chris Lattner7dfe8fd2011-01-16 05:08:00 +0000681 } else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) {
682 Constant *C = cast<Constant>(I.getOperand(1));
683 for (unsigned i = 0; i != NumPHIValues; ++i) {
684 Value *InV = 0;
685 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
686 InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
687 else if (isa<ICmpInst>(CI))
688 InV = Builder->CreateICmp(CI->getPredicate(), PN->getIncomingValue(i),
689 C, "phitmp");
690 else
691 InV = Builder->CreateFCmp(CI->getPredicate(), PN->getIncomingValue(i),
692 C, "phitmp");
693 NewPN->addIncoming(InV, PN->getIncomingBlock(i));
694 }
Chris Lattner5d1704d2009-09-27 19:57:57 +0000695 } else if (I.getNumOperands() == 2) {
Chris Lattner4e998b22004-09-29 05:07:12 +0000696 Constant *C = cast<Constant>(I.getOperand(1));
Chris Lattnerbac32862004-11-14 19:13:23 +0000697 for (unsigned i = 0; i != NumPHIValues; ++i) {
Chris Lattnera9ff5eb2007-08-05 08:47:58 +0000698 Value *InV = 0;
Chris Lattner7dfe8fd2011-01-16 05:08:00 +0000699 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
700 InV = ConstantExpr::get(I.getOpcode(), InC, C);
701 else
702 InV = Builder->CreateBinOp(cast<BinaryOperator>(I).getOpcode(),
703 PN->getIncomingValue(i), C, "phitmp");
Chris Lattner2a86f3b2006-09-09 22:02:56 +0000704 NewPN->addIncoming(InV, PN->getIncomingBlock(i));
Chris Lattner4e998b22004-09-29 05:07:12 +0000705 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000706 } else {
707 CastInst *CI = cast<CastInst>(&I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000708 Type *RetTy = CI->getType();
Chris Lattnerbac32862004-11-14 19:13:23 +0000709 for (unsigned i = 0; i != NumPHIValues; ++i) {
Chris Lattner2a86f3b2006-09-09 22:02:56 +0000710 Value *InV;
Chris Lattner7dfe8fd2011-01-16 05:08:00 +0000711 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
Owen Andersonbaf3c402009-07-29 18:55:55 +0000712 InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
Chris Lattner7dfe8fd2011-01-16 05:08:00 +0000713 else
714 InV = Builder->CreateCast(CI->getOpcode(),
715 PN->getIncomingValue(i), I.getType(), "phitmp");
Chris Lattner2a86f3b2006-09-09 22:02:56 +0000716 NewPN->addIncoming(InV, PN->getIncomingBlock(i));
Chris Lattner4e998b22004-09-29 05:07:12 +0000717 }
718 }
Chris Lattner192228e2011-01-16 05:28:59 +0000719
720 for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
721 UI != E; ) {
722 Instruction *User = cast<Instruction>(*UI++);
723 if (User == &I) continue;
724 ReplaceInstUsesWith(*User, NewPN);
725 EraseInstFromFunction(*User);
726 }
Chris Lattner4e998b22004-09-29 05:07:12 +0000727 return ReplaceInstUsesWith(I, NewPN);
728}
729
Chris Lattner46cd5a12009-01-09 05:44:56 +0000730/// FindElementAtOffset - Given a type and a constant offset, determine whether
731/// or not there is a sequence of GEP indices into the type that will land us at
Chris Lattner3914f722009-01-24 01:00:13 +0000732/// the specified offset. If so, fill them into NewIndices and return the
733/// resultant element type, otherwise return null.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000734Type *InstCombiner::FindElementAtOffset(Type *Ty, int64_t Offset,
Chris Lattner80f43d32010-01-04 07:53:58 +0000735 SmallVectorImpl<Value*> &NewIndices) {
Dan Gohmance9fe9f2009-07-21 23:21:54 +0000736 if (!TD) return 0;
Chris Lattner3914f722009-01-24 01:00:13 +0000737 if (!Ty->isSized()) return 0;
Chris Lattner46cd5a12009-01-09 05:44:56 +0000738
739 // Start with the index over the outer type. Note that the type size
740 // might be zero (even if the offset isn't zero) if the indexed type
741 // is something like [0 x {int, int}]
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000742 Type *IntPtrTy = TD->getIntPtrType(Ty->getContext());
Chris Lattner46cd5a12009-01-09 05:44:56 +0000743 int64_t FirstIdx = 0;
Duncan Sands777d2302009-05-09 07:06:46 +0000744 if (int64_t TySize = TD->getTypeAllocSize(Ty)) {
Chris Lattner46cd5a12009-01-09 05:44:56 +0000745 FirstIdx = Offset/TySize;
Chris Lattner31a69cb2009-01-11 20:41:36 +0000746 Offset -= FirstIdx*TySize;
Chris Lattner46cd5a12009-01-09 05:44:56 +0000747
Chris Lattnerdbc3bc22009-01-11 20:15:20 +0000748 // Handle hosts where % returns negative instead of values [0..TySize).
Chris Lattner46cd5a12009-01-09 05:44:56 +0000749 if (Offset < 0) {
750 --FirstIdx;
751 Offset += TySize;
752 assert(Offset >= 0);
753 }
754 assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
755 }
756
Owen Andersoneed707b2009-07-24 23:12:02 +0000757 NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
Chris Lattner46cd5a12009-01-09 05:44:56 +0000758
759 // Index into the types. If we fail, set OrigBase to null.
760 while (Offset) {
Chris Lattnerdbc3bc22009-01-11 20:15:20 +0000761 // Indexing into tail padding between struct/array elements.
762 if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty))
Chris Lattner3914f722009-01-24 01:00:13 +0000763 return 0;
Chris Lattnerdbc3bc22009-01-11 20:15:20 +0000764
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000765 if (StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner46cd5a12009-01-09 05:44:56 +0000766 const StructLayout *SL = TD->getStructLayout(STy);
Chris Lattnerdbc3bc22009-01-11 20:15:20 +0000767 assert(Offset < (int64_t)SL->getSizeInBytes() &&
768 "Offset must stay within the indexed type");
769
Chris Lattner46cd5a12009-01-09 05:44:56 +0000770 unsigned Elt = SL->getElementContainingOffset(Offset);
Chris Lattner4de84762010-01-04 07:02:48 +0000771 NewIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
772 Elt));
Chris Lattner46cd5a12009-01-09 05:44:56 +0000773
774 Offset -= SL->getElementOffset(Elt);
775 Ty = STy->getElementType(Elt);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000776 } else if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
Duncan Sands777d2302009-05-09 07:06:46 +0000777 uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType());
Chris Lattnerdbc3bc22009-01-11 20:15:20 +0000778 assert(EltSize && "Cannot index into a zero-sized array");
Owen Andersoneed707b2009-07-24 23:12:02 +0000779 NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
Chris Lattnerdbc3bc22009-01-11 20:15:20 +0000780 Offset %= EltSize;
Chris Lattner1c412d92009-01-11 20:23:52 +0000781 Ty = AT->getElementType();
Chris Lattner46cd5a12009-01-09 05:44:56 +0000782 } else {
Chris Lattnerdbc3bc22009-01-11 20:15:20 +0000783 // Otherwise, we can't index into the middle of this atomic type, bail.
Chris Lattner3914f722009-01-24 01:00:13 +0000784 return 0;
Chris Lattner46cd5a12009-01-09 05:44:56 +0000785 }
786 }
787
Chris Lattner3914f722009-01-24 01:00:13 +0000788 return Ty;
Chris Lattner46cd5a12009-01-09 05:44:56 +0000789}
790
Rafael Espindola592ad6a2011-07-31 04:43:41 +0000791static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src) {
792 // If this GEP has only 0 indices, it is the same pointer as
793 // Src. If Src is not a trivial GEP too, don't combine
794 // the indices.
795 if (GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&
796 !Src.hasOneUse())
797 return false;
798 return true;
799}
Chris Lattner473945d2002-05-06 18:06:38 +0000800
Chris Lattner7e708292002-06-25 16:13:24 +0000801Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000802 SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end());
803
Jay Foadb9b54eb2011-07-19 15:07:52 +0000804 if (Value *V = SimplifyGEPInst(Ops, TD))
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000805 return ReplaceInstUsesWith(GEP, V);
806
Chris Lattner620ce142004-05-07 22:09:22 +0000807 Value *PtrOp = GEP.getOperand(0);
Chris Lattnerc6bd1952004-02-22 05:25:17 +0000808
Duncan Sandsa63395a2010-11-22 16:32:50 +0000809 // Eliminate unneeded casts for indices, and replace indices which displace
810 // by multiples of a zero size type with zero.
Chris Lattnerccf4b342009-08-30 04:49:01 +0000811 if (TD) {
812 bool MadeChange = false;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000813 Type *IntPtrTy = TD->getIntPtrType(GEP.getContext());
Duncan Sandsa63395a2010-11-22 16:32:50 +0000814
Chris Lattnerccf4b342009-08-30 04:49:01 +0000815 gep_type_iterator GTI = gep_type_begin(GEP);
816 for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end();
817 I != E; ++I, ++GTI) {
Duncan Sandsa63395a2010-11-22 16:32:50 +0000818 // Skip indices into struct types.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000819 SequentialType *SeqTy = dyn_cast<SequentialType>(*GTI);
Duncan Sandsa63395a2010-11-22 16:32:50 +0000820 if (!SeqTy) continue;
821
822 // If the element type has zero size then any index over it is equivalent
823 // to an index of zero, so replace it with zero if it is not zero already.
824 if (SeqTy->getElementType()->isSized() &&
825 TD->getTypeAllocSize(SeqTy->getElementType()) == 0)
826 if (!isa<Constant>(*I) || !cast<Constant>(*I)->isNullValue()) {
827 *I = Constant::getNullValue(IntPtrTy);
828 MadeChange = true;
829 }
830
831 if ((*I)->getType() != IntPtrTy) {
832 // If we are using a wider index than needed for this platform, shrink
833 // it to what we need. If narrower, sign-extend it to what we need.
834 // This explicit cast can make subsequent optimizations more obvious.
835 *I = Builder->CreateIntCast(*I, IntPtrTy, true);
836 MadeChange = true;
837 }
Chris Lattner28977af2004-04-05 01:30:19 +0000838 }
Chris Lattnerccf4b342009-08-30 04:49:01 +0000839 if (MadeChange) return &GEP;
Chris Lattnerdb9654e2007-03-25 20:43:09 +0000840 }
Chris Lattner28977af2004-04-05 01:30:19 +0000841
Chris Lattner90ac28c2002-08-02 19:29:35 +0000842 // Combine Indices - If the source pointer to this getelementptr instruction
843 // is a getelementptr instruction, combine the indices of the two
844 // getelementptr instructions into a single instruction.
845 //
Dan Gohmand6aa02d2009-07-28 01:40:03 +0000846 if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) {
Rafael Espindola592ad6a2011-07-31 04:43:41 +0000847 if (!shouldMergeGEPs(*cast<GEPOperator>(&GEP), *Src))
Rafael Espindolab5a12dd2011-07-11 03:43:47 +0000848 return 0;
849
Chris Lattner620ce142004-05-07 22:09:22 +0000850 // Note that if our source is a gep chain itself that we wait for that
851 // chain to be resolved before we perform this transformation. This
852 // avoids us creating a TON of code in some cases.
Rafael Espindola592ad6a2011-07-31 04:43:41 +0000853 if (GEPOperator *SrcGEP =
854 dyn_cast<GEPOperator>(Src->getOperand(0)))
855 if (SrcGEP->getNumOperands() == 2 && shouldMergeGEPs(*Src, *SrcGEP))
Chris Lattnerf9b91bb2009-08-30 05:08:50 +0000856 return 0; // Wait until our source is folded to completion.
Chris Lattner620ce142004-05-07 22:09:22 +0000857
Chris Lattner72588fc2007-02-15 22:48:32 +0000858 SmallVector<Value*, 8> Indices;
Chris Lattner620ce142004-05-07 22:09:22 +0000859
860 // Find out whether the last index in the source GEP is a sequential idx.
861 bool EndsWithSequential = false;
Chris Lattnerab984842009-08-30 05:30:55 +0000862 for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src);
863 I != E; ++I)
Duncan Sands1df98592010-02-16 11:11:14 +0000864 EndsWithSequential = !(*I)->isStructTy();
Misha Brukmanfd939082005-04-21 23:48:37 +0000865
Chris Lattner90ac28c2002-08-02 19:29:35 +0000866 // Can we combine the two pointer arithmetics offsets?
Chris Lattner620ce142004-05-07 22:09:22 +0000867 if (EndsWithSequential) {
Chris Lattnerdecd0812003-03-05 22:33:14 +0000868 // Replace: gep (gep %P, long B), long A, ...
869 // With: T = long A+B; gep %P, T, ...
870 //
Chris Lattnerf9b91bb2009-08-30 05:08:50 +0000871 Value *Sum;
872 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
873 Value *GO1 = GEP.getOperand(1);
Owen Andersona7235ea2009-07-31 20:28:14 +0000874 if (SO1 == Constant::getNullValue(SO1->getType())) {
Chris Lattner28977af2004-04-05 01:30:19 +0000875 Sum = GO1;
Owen Andersona7235ea2009-07-31 20:28:14 +0000876 } else if (GO1 == Constant::getNullValue(GO1->getType())) {
Chris Lattner28977af2004-04-05 01:30:19 +0000877 Sum = SO1;
878 } else {
Chris Lattnerab984842009-08-30 05:30:55 +0000879 // If they aren't the same type, then the input hasn't been processed
880 // by the loop above yet (which canonicalizes sequential index types to
881 // intptr_t). Just avoid transforming this until the input has been
882 // normalized.
883 if (SO1->getType() != GO1->getType())
884 return 0;
Chris Lattnerf925cbd2009-08-30 18:50:58 +0000885 Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
Chris Lattner28977af2004-04-05 01:30:19 +0000886 }
Chris Lattner620ce142004-05-07 22:09:22 +0000887
Chris Lattnerab984842009-08-30 05:30:55 +0000888 // Update the GEP in place if possible.
Chris Lattnerf9b91bb2009-08-30 05:08:50 +0000889 if (Src->getNumOperands() == 2) {
890 GEP.setOperand(0, Src->getOperand(0));
Chris Lattner620ce142004-05-07 22:09:22 +0000891 GEP.setOperand(1, Sum);
892 return &GEP;
Chris Lattner620ce142004-05-07 22:09:22 +0000893 }
Chris Lattnerab984842009-08-30 05:30:55 +0000894 Indices.append(Src->op_begin()+1, Src->op_end()-1);
Chris Lattnerccf4b342009-08-30 04:49:01 +0000895 Indices.push_back(Sum);
Chris Lattnerab984842009-08-30 05:30:55 +0000896 Indices.append(GEP.op_begin()+2, GEP.op_end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000897 } else if (isa<Constant>(*GEP.idx_begin()) &&
Chris Lattner28977af2004-04-05 01:30:19 +0000898 cast<Constant>(*GEP.idx_begin())->isNullValue() &&
Chris Lattnerf9b91bb2009-08-30 05:08:50 +0000899 Src->getNumOperands() != 1) {
Chris Lattner90ac28c2002-08-02 19:29:35 +0000900 // Otherwise we can do the fold if the first index of the GEP is a zero
Chris Lattnerab984842009-08-30 05:30:55 +0000901 Indices.append(Src->op_begin()+1, Src->op_end());
902 Indices.append(GEP.idx_begin()+1, GEP.idx_end());
Chris Lattner90ac28c2002-08-02 19:29:35 +0000903 }
904
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000905 if (!Indices.empty())
Chris Lattner948cdeb2010-01-05 07:42:10 +0000906 return (GEP.isInBounds() && Src->isInBounds()) ?
Jay Foada9203102011-07-25 09:48:08 +0000907 GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices,
908 GEP.getName()) :
909 GetElementPtrInst::Create(Src->getOperand(0), Indices, GEP.getName());
Chris Lattner6e24d832009-08-30 05:00:50 +0000910 }
Nadav Rotem0286ca82011-04-05 14:29:52 +0000911
Chris Lattnerf9b91bb2009-08-30 05:08:50 +0000912 // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
Chris Lattner948cdeb2010-01-05 07:42:10 +0000913 Value *StrippedPtr = PtrOp->stripPointerCasts();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000914 PointerType *StrippedPtrTy =cast<PointerType>(StrippedPtr->getType());
Nadav Rotem0286ca82011-04-05 14:29:52 +0000915 if (StrippedPtr != PtrOp &&
916 StrippedPtrTy->getAddressSpace() == GEP.getPointerAddressSpace()) {
Chris Lattner963f4ba2009-08-30 20:36:46 +0000917
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000918 bool HasZeroPointerIndex = false;
919 if (ConstantInt *C = dyn_cast<ConstantInt>(GEP.getOperand(1)))
920 HasZeroPointerIndex = C->isZero();
Nadav Rotem0286ca82011-04-05 14:29:52 +0000921
Chris Lattner963f4ba2009-08-30 20:36:46 +0000922 // Transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
923 // into : GEP [10 x i8]* X, i32 0, ...
924 //
925 // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
926 // into : GEP i8* X, ...
Nadav Rotem0286ca82011-04-05 14:29:52 +0000927 //
Chris Lattner963f4ba2009-08-30 20:36:46 +0000928 // This occurs when the program declares an array extern like "int X[];"
Chris Lattner6e24d832009-08-30 05:00:50 +0000929 if (HasZeroPointerIndex) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000930 PointerType *CPTy = cast<PointerType>(PtrOp->getType());
931 if (ArrayType *CATy =
Duncan Sands5b7cfb02009-03-02 09:18:21 +0000932 dyn_cast<ArrayType>(CPTy->getElementType())) {
933 // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
Chris Lattner948cdeb2010-01-05 07:42:10 +0000934 if (CATy->getElementType() == StrippedPtrTy->getElementType()) {
Duncan Sands5b7cfb02009-03-02 09:18:21 +0000935 // -> GEP i8* X, ...
Chris Lattner948cdeb2010-01-05 07:42:10 +0000936 SmallVector<Value*, 8> Idx(GEP.idx_begin()+1, GEP.idx_end());
937 GetElementPtrInst *Res =
Jay Foada9203102011-07-25 09:48:08 +0000938 GetElementPtrInst::Create(StrippedPtr, Idx, GEP.getName());
Chris Lattner948cdeb2010-01-05 07:42:10 +0000939 Res->setIsInBounds(GEP.isInBounds());
940 return Res;
Chris Lattner963f4ba2009-08-30 20:36:46 +0000941 }
942
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000943 if (ArrayType *XATy =
Chris Lattner948cdeb2010-01-05 07:42:10 +0000944 dyn_cast<ArrayType>(StrippedPtrTy->getElementType())){
Duncan Sands5b7cfb02009-03-02 09:18:21 +0000945 // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
Chris Lattnereed48272005-09-13 00:40:14 +0000946 if (CATy->getElementType() == XATy->getElementType()) {
Duncan Sands5b7cfb02009-03-02 09:18:21 +0000947 // -> GEP [10 x i8]* X, i32 0, ...
Chris Lattnereed48272005-09-13 00:40:14 +0000948 // At this point, we know that the cast source type is a pointer
949 // to an array of the same type as the destination pointer
950 // array. Because the array type is never stepped over (there
951 // is a leading zero) we can fold the cast into this GEP.
Chris Lattner948cdeb2010-01-05 07:42:10 +0000952 GEP.setOperand(0, StrippedPtr);
Chris Lattnereed48272005-09-13 00:40:14 +0000953 return &GEP;
954 }
Duncan Sands5b7cfb02009-03-02 09:18:21 +0000955 }
956 }
Chris Lattnereed48272005-09-13 00:40:14 +0000957 } else if (GEP.getNumOperands() == 2) {
958 // Transform things like:
Wojciech Matyjewiczed223252007-12-12 15:21:32 +0000959 // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
960 // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000961 Type *SrcElTy = StrippedPtrTy->getElementType();
962 Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
Duncan Sands1df98592010-02-16 11:11:14 +0000963 if (TD && SrcElTy->isArrayTy() &&
Duncan Sands777d2302009-05-09 07:06:46 +0000964 TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
965 TD->getTypeAllocSize(ResElTy)) {
David Greeneb8f74792007-09-04 15:46:09 +0000966 Value *Idx[2];
Chris Lattner4de84762010-01-04 07:02:48 +0000967 Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext()));
David Greeneb8f74792007-09-04 15:46:09 +0000968 Idx[1] = GEP.getOperand(1);
Chris Lattner948cdeb2010-01-05 07:42:10 +0000969 Value *NewGEP = GEP.isInBounds() ?
Jay Foad0a2a60a2011-07-22 08:16:57 +0000970 Builder->CreateInBoundsGEP(StrippedPtr, Idx, GEP.getName()) :
971 Builder->CreateGEP(StrippedPtr, Idx, GEP.getName());
Reid Spencer3da59db2006-11-27 01:05:10 +0000972 // V and GEP are both pointer types --> BitCast
Chris Lattnerf925cbd2009-08-30 18:50:58 +0000973 return new BitCastInst(NewGEP, GEP.getType());
Chris Lattnerc6bd1952004-02-22 05:25:17 +0000974 }
Chris Lattner7835cdd2005-09-13 18:36:04 +0000975
976 // Transform things like:
Wojciech Matyjewiczed223252007-12-12 15:21:32 +0000977 // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
Chris Lattner7835cdd2005-09-13 18:36:04 +0000978 // (where tmp = 8*tmp2) into:
Wojciech Matyjewiczed223252007-12-12 15:21:32 +0000979 // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
Chris Lattner7835cdd2005-09-13 18:36:04 +0000980
Duncan Sands1df98592010-02-16 11:11:14 +0000981 if (TD && SrcElTy->isArrayTy() && ResElTy->isIntegerTy(8)) {
Chris Lattner7835cdd2005-09-13 18:36:04 +0000982 uint64_t ArrayEltSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000983 TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType());
Chris Lattner7835cdd2005-09-13 18:36:04 +0000984
985 // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We
986 // allow either a mul, shift, or constant here.
987 Value *NewIdx = 0;
988 ConstantInt *Scale = 0;
989 if (ArrayEltSize == 1) {
990 NewIdx = GEP.getOperand(1);
Chris Lattnerab984842009-08-30 05:30:55 +0000991 Scale = ConstantInt::get(cast<IntegerType>(NewIdx->getType()), 1);
Chris Lattner7835cdd2005-09-13 18:36:04 +0000992 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000993 NewIdx = ConstantInt::get(CI->getType(), 1);
Chris Lattner7835cdd2005-09-13 18:36:04 +0000994 Scale = CI;
995 } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
996 if (Inst->getOpcode() == Instruction::Shl &&
997 isa<ConstantInt>(Inst->getOperand(1))) {
Zhou Sheng0e2d3ac2007-03-30 09:29:48 +0000998 ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1));
999 uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
Owen Andersoneed707b2009-07-24 23:12:02 +00001000 Scale = ConstantInt::get(cast<IntegerType>(Inst->getType()),
Dan Gohman6de29f82009-06-15 22:12:54 +00001001 1ULL << ShAmtVal);
Chris Lattner7835cdd2005-09-13 18:36:04 +00001002 NewIdx = Inst->getOperand(0);
1003 } else if (Inst->getOpcode() == Instruction::Mul &&
1004 isa<ConstantInt>(Inst->getOperand(1))) {
1005 Scale = cast<ConstantInt>(Inst->getOperand(1));
1006 NewIdx = Inst->getOperand(0);
1007 }
1008 }
Wojciech Matyjewiczed223252007-12-12 15:21:32 +00001009
Chris Lattner7835cdd2005-09-13 18:36:04 +00001010 // If the index will be to exactly the right offset with the scale taken
Wojciech Matyjewiczed223252007-12-12 15:21:32 +00001011 // out, perform the transformation. Note, we don't know whether Scale is
1012 // signed or not. We'll use unsigned version of division/modulo
1013 // operation after making sure Scale doesn't have the sign bit set.
Chris Lattner58b1ac72009-02-25 18:20:01 +00001014 if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
Wojciech Matyjewiczed223252007-12-12 15:21:32 +00001015 Scale->getZExtValue() % ArrayEltSize == 0) {
Owen Andersoneed707b2009-07-24 23:12:02 +00001016 Scale = ConstantInt::get(Scale->getType(),
Wojciech Matyjewiczed223252007-12-12 15:21:32 +00001017 Scale->getZExtValue() / ArrayEltSize);
Reid Spencerb83eb642006-10-20 07:07:24 +00001018 if (Scale->getZExtValue() != 1) {
Chris Lattner878daed2009-08-30 05:56:44 +00001019 Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
1020 false /*ZExt*/);
Chris Lattnerf925cbd2009-08-30 18:50:58 +00001021 NewIdx = Builder->CreateMul(NewIdx, C, "idxscale");
Chris Lattner7835cdd2005-09-13 18:36:04 +00001022 }
1023
1024 // Insert the new GEP instruction.
David Greeneb8f74792007-09-04 15:46:09 +00001025 Value *Idx[2];
Chris Lattner4de84762010-01-04 07:02:48 +00001026 Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext()));
David Greeneb8f74792007-09-04 15:46:09 +00001027 Idx[1] = NewIdx;
Chris Lattner948cdeb2010-01-05 07:42:10 +00001028 Value *NewGEP = GEP.isInBounds() ?
Jay Foad0a2a60a2011-07-22 08:16:57 +00001029 Builder->CreateInBoundsGEP(StrippedPtr, Idx, GEP.getName()):
1030 Builder->CreateGEP(StrippedPtr, Idx, GEP.getName());
Reid Spencer3da59db2006-11-27 01:05:10 +00001031 // The NewGEP must be pointer typed, so must the old one -> BitCast
1032 return new BitCastInst(NewGEP, GEP.getType());
Chris Lattner7835cdd2005-09-13 18:36:04 +00001033 }
1034 }
Chris Lattnerc6bd1952004-02-22 05:25:17 +00001035 }
Chris Lattner8a2a3112001-12-14 16:52:21 +00001036 }
Nadav Rotem0286ca82011-04-05 14:29:52 +00001037
Chris Lattner46cd5a12009-01-09 05:44:56 +00001038 /// See if we can simplify:
Chris Lattner873ff012009-08-30 05:55:36 +00001039 /// X = bitcast A* to B*
Chris Lattner46cd5a12009-01-09 05:44:56 +00001040 /// Y = gep X, <...constant indices...>
1041 /// into a gep of the original struct. This is important for SROA and alias
1042 /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged.
Chris Lattner58407792009-01-09 04:53:57 +00001043 if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
Dan Gohmance9fe9f2009-07-21 23:21:54 +00001044 if (TD &&
Nadav Rotem0286ca82011-04-05 14:29:52 +00001045 !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices() &&
1046 StrippedPtrTy->getAddressSpace() == GEP.getPointerAddressSpace()) {
1047
Chris Lattner46cd5a12009-01-09 05:44:56 +00001048 // Determine how much the GEP moves the pointer. We are guaranteed to get
1049 // a constant back from EmitGEPOffset.
Chris Lattner02446fc2010-01-04 07:37:31 +00001050 ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(&GEP));
Chris Lattner46cd5a12009-01-09 05:44:56 +00001051 int64_t Offset = OffsetV->getSExtValue();
Nadav Rotem0286ca82011-04-05 14:29:52 +00001052
Chris Lattner46cd5a12009-01-09 05:44:56 +00001053 // If this GEP instruction doesn't move the pointer, just replace the GEP
1054 // with a bitcast of the real input to the dest type.
1055 if (Offset == 0) {
1056 // If the bitcast is of an allocation, and the allocation will be
1057 // converted to match the type of the cast, don't touch this.
Victor Hernandez7b929da2009-10-23 21:09:37 +00001058 if (isa<AllocaInst>(BCI->getOperand(0)) ||
Victor Hernandez83d63912009-09-18 22:35:49 +00001059 isMalloc(BCI->getOperand(0))) {
Chris Lattner46cd5a12009-01-09 05:44:56 +00001060 // See if the bitcast simplifies, if so, don't nuke this GEP yet.
1061 if (Instruction *I = visitBitCast(*BCI)) {
1062 if (I != BCI) {
1063 I->takeName(BCI);
1064 BCI->getParent()->getInstList().insert(BCI, I);
1065 ReplaceInstUsesWith(*BCI, I);
1066 }
1067 return &GEP;
Chris Lattner58407792009-01-09 04:53:57 +00001068 }
Chris Lattner58407792009-01-09 04:53:57 +00001069 }
Chris Lattner46cd5a12009-01-09 05:44:56 +00001070 return new BitCastInst(BCI->getOperand(0), GEP.getType());
Chris Lattner58407792009-01-09 04:53:57 +00001071 }
Chris Lattner46cd5a12009-01-09 05:44:56 +00001072
1073 // Otherwise, if the offset is non-zero, we need to find out if there is a
1074 // field at Offset in 'A's type. If so, we can pull the cast through the
1075 // GEP.
1076 SmallVector<Value*, 8> NewIndices;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001077 Type *InTy =
Chris Lattner46cd5a12009-01-09 05:44:56 +00001078 cast<PointerType>(BCI->getOperand(0)->getType())->getElementType();
Chris Lattner80f43d32010-01-04 07:53:58 +00001079 if (FindElementAtOffset(InTy, Offset, NewIndices)) {
Chris Lattner948cdeb2010-01-05 07:42:10 +00001080 Value *NGEP = GEP.isInBounds() ?
Jay Foad0a2a60a2011-07-22 08:16:57 +00001081 Builder->CreateInBoundsGEP(BCI->getOperand(0), NewIndices) :
1082 Builder->CreateGEP(BCI->getOperand(0), NewIndices);
Chris Lattnerf925cbd2009-08-30 18:50:58 +00001083
1084 if (NGEP->getType() == GEP.getType())
1085 return ReplaceInstUsesWith(GEP, NGEP);
Chris Lattner46cd5a12009-01-09 05:44:56 +00001086 NGEP->takeName(&GEP);
1087 return new BitCastInst(NGEP, GEP.getType());
1088 }
Chris Lattner58407792009-01-09 04:53:57 +00001089 }
1090 }
1091
Chris Lattner8a2a3112001-12-14 16:52:21 +00001092 return 0;
1093}
1094
Duncan Sands1d9b9732010-05-27 19:09:06 +00001095
1096
Nick Lewyckydbd22552011-08-03 01:11:40 +00001097static bool IsOnlyNullComparedAndFreed(Value *V, SmallVectorImpl<WeakVH> &Users,
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001098 int Depth = 0) {
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001099 if (Depth == 8)
1100 return false;
1101
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001102 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end();
Duncan Sands1d9b9732010-05-27 19:09:06 +00001103 UI != UE; ++UI) {
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001104 User *U = *UI;
1105 if (isFreeCall(U)) {
1106 Users.push_back(U);
Duncan Sands1d9b9732010-05-27 19:09:06 +00001107 continue;
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001108 }
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001109 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U)) {
1110 if (ICI->isEquality() && isa<ConstantPointerNull>(ICI->getOperand(1))) {
1111 Users.push_back(ICI);
1112 continue;
1113 }
1114 }
1115 if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
1116 if (IsOnlyNullComparedAndFreed(BCI, Users, Depth+1)) {
1117 Users.push_back(BCI);
1118 continue;
1119 }
1120 }
1121 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
Nick Lewyckydbd22552011-08-03 01:11:40 +00001122 if (IsOnlyNullComparedAndFreed(GEPI, Users, Depth+1)) {
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001123 Users.push_back(GEPI);
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001124 continue;
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001125 }
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001126 }
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001127 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001128 if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001129 II->getIntrinsicID() == Intrinsic::lifetime_end) {
1130 Users.push_back(II);
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001131 continue;
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001132 }
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001133 }
Duncan Sands1d9b9732010-05-27 19:09:06 +00001134 return false;
1135 }
1136 return true;
1137}
1138
1139Instruction *InstCombiner::visitMalloc(Instruction &MI) {
1140 // If we have a malloc call which is only used in any amount of comparisons
1141 // to null and free calls, delete the calls and replace the comparisons with
1142 // true or false as appropriate.
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001143 SmallVector<WeakVH, 64> Users;
1144 if (IsOnlyNullComparedAndFreed(&MI, Users)) {
1145 for (unsigned i = 0, e = Users.size(); i != e; ++i) {
1146 Instruction *I = cast_or_null<Instruction>(&*Users[i]);
1147 if (!I) continue;
Duncan Sands1d9b9732010-05-27 19:09:06 +00001148
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001149 if (ICmpInst *C = dyn_cast<ICmpInst>(I)) {
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001150 ReplaceInstUsesWith(*C,
1151 ConstantInt::get(Type::getInt1Ty(C->getContext()),
1152 C->isFalseWhenEqual()));
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001153 } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I)) {
Nick Lewyckyd8030c72011-08-02 22:08:01 +00001154 ReplaceInstUsesWith(*I, UndefValue::get(I->getType()));
Duncan Sands1d9b9732010-05-27 19:09:06 +00001155 }
Nick Lewyckyd5061a92011-08-03 00:43:35 +00001156 EraseInstFromFunction(*I);
Duncan Sands1d9b9732010-05-27 19:09:06 +00001157 }
1158 return EraseInstFromFunction(MI);
1159 }
1160 return 0;
1161}
1162
1163
1164
Gabor Greif91697372010-06-24 12:21:15 +00001165Instruction *InstCombiner::visitFree(CallInst &FI) {
1166 Value *Op = FI.getArgOperand(0);
Victor Hernandez66284e02009-10-24 04:23:03 +00001167
1168 // free undef -> unreachable.
1169 if (isa<UndefValue>(Op)) {
1170 // Insert a new store to null because we cannot modify the CFG here.
Eli Friedmane6f364b2011-05-18 23:58:37 +00001171 Builder->CreateStore(ConstantInt::getTrue(FI.getContext()),
1172 UndefValue::get(Type::getInt1PtrTy(FI.getContext())));
Victor Hernandez66284e02009-10-24 04:23:03 +00001173 return EraseInstFromFunction(FI);
1174 }
1175
1176 // If we have 'free null' delete the instruction. This can happen in stl code
1177 // when lots of inlining happens.
1178 if (isa<ConstantPointerNull>(Op))
1179 return EraseInstFromFunction(FI);
1180
Victor Hernandez66284e02009-10-24 04:23:03 +00001181 return 0;
1182}
Chris Lattner67b1e1b2003-12-07 01:24:23 +00001183
Chris Lattner3284d1f2007-04-15 00:07:55 +00001184
Chris Lattner2f503e62005-01-31 05:36:43 +00001185
Chris Lattnerc4d10eb2003-06-04 04:46:00 +00001186Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
1187 // Change br (not X), label True, label False to: br X, label False, True
Reid Spencer4b828e62005-06-18 17:37:34 +00001188 Value *X = 0;
Chris Lattneracd1f0f2004-07-30 07:50:03 +00001189 BasicBlock *TrueDest;
1190 BasicBlock *FalseDest;
Dan Gohman4ae51262009-08-12 16:23:25 +00001191 if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
Chris Lattneracd1f0f2004-07-30 07:50:03 +00001192 !isa<Constant>(X)) {
1193 // Swap Destinations and condition...
1194 BI.setCondition(X);
Chandler Carruth602650c2011-10-17 01:11:57 +00001195 BI.swapSuccessors();
Chris Lattneracd1f0f2004-07-30 07:50:03 +00001196 return &BI;
1197 }
1198
Reid Spencere4d87aa2006-12-23 06:05:41 +00001199 // Cannonicalize fcmp_one -> fcmp_oeq
1200 FCmpInst::Predicate FPred; Value *Y;
1201 if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
Chris Lattner7a1e9242009-08-30 06:13:40 +00001202 TrueDest, FalseDest)) &&
1203 BI.getCondition()->hasOneUse())
1204 if (FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
1205 FPred == FCmpInst::FCMP_OGE) {
1206 FCmpInst *Cond = cast<FCmpInst>(BI.getCondition());
1207 Cond->setPredicate(FCmpInst::getInversePredicate(FPred));
1208
1209 // Swap Destinations and condition.
Chandler Carruth602650c2011-10-17 01:11:57 +00001210 BI.swapSuccessors();
Chris Lattner7a1e9242009-08-30 06:13:40 +00001211 Worklist.Add(Cond);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001212 return &BI;
1213 }
1214
1215 // Cannonicalize icmp_ne -> icmp_eq
1216 ICmpInst::Predicate IPred;
1217 if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
Chris Lattner7a1e9242009-08-30 06:13:40 +00001218 TrueDest, FalseDest)) &&
1219 BI.getCondition()->hasOneUse())
1220 if (IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE ||
1221 IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
1222 IPred == ICmpInst::ICMP_SGE) {
1223 ICmpInst *Cond = cast<ICmpInst>(BI.getCondition());
1224 Cond->setPredicate(ICmpInst::getInversePredicate(IPred));
1225 // Swap Destinations and condition.
Chandler Carruth602650c2011-10-17 01:11:57 +00001226 BI.swapSuccessors();
Chris Lattner7a1e9242009-08-30 06:13:40 +00001227 Worklist.Add(Cond);
Chris Lattner40f5d702003-06-04 05:10:11 +00001228 return &BI;
1229 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001230
Chris Lattnerc4d10eb2003-06-04 04:46:00 +00001231 return 0;
1232}
Chris Lattner0864acf2002-11-04 16:18:53 +00001233
Chris Lattner46238a62004-07-03 00:26:11 +00001234Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
1235 Value *Cond = SI.getCondition();
1236 if (Instruction *I = dyn_cast<Instruction>(Cond)) {
1237 if (I->getOpcode() == Instruction::Add)
1238 if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
1239 // change 'switch (X+4) case 1:' into 'switch (X) case -3'
Eli Friedmanbb5a7442011-09-29 20:21:17 +00001240 unsigned NumCases = SI.getNumCases();
1241 // Skip the first item since that's the default case.
1242 for (unsigned i = 1; i < NumCases; ++i) {
1243 ConstantInt* CaseVal = SI.getCaseValue(i);
1244 Constant* NewCaseVal = ConstantExpr::getSub(cast<Constant>(CaseVal),
1245 AddRHS);
1246 assert(isa<ConstantInt>(NewCaseVal) &&
1247 "Result of expression should be constant");
1248 SI.setSuccessorValue(i, cast<ConstantInt>(NewCaseVal));
1249 }
1250 SI.setCondition(I->getOperand(0));
Chris Lattner7a1e9242009-08-30 06:13:40 +00001251 Worklist.Add(I);
Chris Lattner46238a62004-07-03 00:26:11 +00001252 return &SI;
1253 }
1254 }
1255 return 0;
1256}
1257
Matthijs Kooijmana9012ec2008-06-11 14:05:05 +00001258Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) {
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001259 Value *Agg = EV.getAggregateOperand();
Matthijs Kooijmana9012ec2008-06-11 14:05:05 +00001260
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001261 if (!EV.hasIndices())
1262 return ReplaceInstUsesWith(EV, Agg);
1263
1264 if (Constant *C = dyn_cast<Constant>(Agg)) {
1265 if (isa<UndefValue>(C))
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001266 return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType()));
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001267
1268 if (isa<ConstantAggregateZero>(C))
Owen Andersona7235ea2009-07-31 20:28:14 +00001269 return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType()));
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001270
1271 if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) {
1272 // Extract the element indexed by the first index out of the constant
1273 Value *V = C->getOperand(*EV.idx_begin());
1274 if (EV.getNumIndices() > 1)
1275 // Extract the remaining indices out of the constant indexed by the
1276 // first index
Jay Foadfc6d3a42011-07-13 10:26:04 +00001277 return ExtractValueInst::Create(V, EV.getIndices().slice(1));
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001278 else
1279 return ReplaceInstUsesWith(EV, V);
1280 }
1281 return 0; // Can't handle other constants
1282 }
1283 if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
1284 // We're extracting from an insertvalue instruction, compare the indices
1285 const unsigned *exti, *exte, *insi, *inse;
1286 for (exti = EV.idx_begin(), insi = IV->idx_begin(),
1287 exte = EV.idx_end(), inse = IV->idx_end();
1288 exti != exte && insi != inse;
1289 ++exti, ++insi) {
1290 if (*insi != *exti)
1291 // The insert and extract both reference distinctly different elements.
1292 // This means the extract is not influenced by the insert, and we can
1293 // replace the aggregate operand of the extract with the aggregate
1294 // operand of the insert. i.e., replace
1295 // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
1296 // %E = extractvalue { i32, { i32 } } %I, 0
1297 // with
1298 // %E = extractvalue { i32, { i32 } } %A, 0
1299 return ExtractValueInst::Create(IV->getAggregateOperand(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001300 EV.getIndices());
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001301 }
1302 if (exti == exte && insi == inse)
1303 // Both iterators are at the end: Index lists are identical. Replace
1304 // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
1305 // %C = extractvalue { i32, { i32 } } %B, 1, 0
1306 // with "i32 42"
1307 return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand());
1308 if (exti == exte) {
1309 // The extract list is a prefix of the insert list. i.e. replace
1310 // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
1311 // %E = extractvalue { i32, { i32 } } %I, 1
1312 // with
1313 // %X = extractvalue { i32, { i32 } } %A, 1
1314 // %E = insertvalue { i32 } %X, i32 42, 0
1315 // by switching the order of the insert and extract (though the
1316 // insertvalue should be left in, since it may have other uses).
Chris Lattnerf925cbd2009-08-30 18:50:58 +00001317 Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001318 EV.getIndices());
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001319 return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
Frits van Bommel39b5abf2011-07-18 12:00:32 +00001320 makeArrayRef(insi, inse));
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001321 }
1322 if (insi == inse)
1323 // The insert list is a prefix of the extract list
1324 // We can simply remove the common indices from the extract and make it
1325 // operate on the inserted value instead of the insertvalue result.
1326 // i.e., replace
1327 // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
1328 // %E = extractvalue { i32, { i32 } } %I, 1, 0
1329 // with
1330 // %E extractvalue { i32 } { i32 42 }, 0
1331 return ExtractValueInst::Create(IV->getInsertedValueOperand(),
Frits van Bommel39b5abf2011-07-18 12:00:32 +00001332 makeArrayRef(exti, exte));
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001333 }
Chris Lattner7e606e22009-11-09 07:07:56 +00001334 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Agg)) {
1335 // We're extracting from an intrinsic, see if we're the only user, which
1336 // allows us to simplify multiple result intrinsics to simpler things that
Gabor Greif91697372010-06-24 12:21:15 +00001337 // just get one value.
Chris Lattner7e606e22009-11-09 07:07:56 +00001338 if (II->hasOneUse()) {
1339 // Check if we're grabbing the overflow bit or the result of a 'with
1340 // overflow' intrinsic. If it's the latter we can remove the intrinsic
1341 // and replace it with a traditional binary instruction.
1342 switch (II->getIntrinsicID()) {
1343 case Intrinsic::uadd_with_overflow:
1344 case Intrinsic::sadd_with_overflow:
1345 if (*EV.idx_begin() == 0) { // Normal result.
Gabor Greif91697372010-06-24 12:21:15 +00001346 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
Eli Friedman3e22cb92011-05-18 00:32:01 +00001347 ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
Chris Lattner7e606e22009-11-09 07:07:56 +00001348 EraseInstFromFunction(*II);
1349 return BinaryOperator::CreateAdd(LHS, RHS);
1350 }
Chris Lattner74b64612010-12-19 19:43:52 +00001351
1352 // If the normal result of the add is dead, and the RHS is a constant,
1353 // we can transform this into a range comparison.
1354 // overflow = uadd a, -4 --> overflow = icmp ugt a, 3
Chris Lattnerf2a97ed2010-12-19 23:24:04 +00001355 if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow)
1356 if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getArgOperand(1)))
1357 return new ICmpInst(ICmpInst::ICMP_UGT, II->getArgOperand(0),
1358 ConstantExpr::getNot(CI));
Chris Lattner7e606e22009-11-09 07:07:56 +00001359 break;
1360 case Intrinsic::usub_with_overflow:
1361 case Intrinsic::ssub_with_overflow:
1362 if (*EV.idx_begin() == 0) { // Normal result.
Gabor Greif91697372010-06-24 12:21:15 +00001363 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
Eli Friedman3e22cb92011-05-18 00:32:01 +00001364 ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
Chris Lattner7e606e22009-11-09 07:07:56 +00001365 EraseInstFromFunction(*II);
1366 return BinaryOperator::CreateSub(LHS, RHS);
1367 }
1368 break;
1369 case Intrinsic::umul_with_overflow:
1370 case Intrinsic::smul_with_overflow:
1371 if (*EV.idx_begin() == 0) { // Normal result.
Gabor Greif91697372010-06-24 12:21:15 +00001372 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
Eli Friedman3e22cb92011-05-18 00:32:01 +00001373 ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
Chris Lattner7e606e22009-11-09 07:07:56 +00001374 EraseInstFromFunction(*II);
1375 return BinaryOperator::CreateMul(LHS, RHS);
1376 }
1377 break;
1378 default:
1379 break;
1380 }
1381 }
1382 }
Frits van Bommel34ceb4d2010-11-29 21:56:20 +00001383 if (LoadInst *L = dyn_cast<LoadInst>(Agg))
1384 // If the (non-volatile) load only has one use, we can rewrite this to a
1385 // load from a GEP. This reduces the size of the load.
1386 // FIXME: If a load is used only by extractvalue instructions then this
1387 // could be done regardless of having multiple uses.
Eli Friedmancc4a0432011-08-15 22:09:40 +00001388 if (L->isSimple() && L->hasOneUse()) {
Frits van Bommel34ceb4d2010-11-29 21:56:20 +00001389 // extractvalue has integer indices, getelementptr has Value*s. Convert.
1390 SmallVector<Value*, 4> Indices;
1391 // Prefix an i32 0 since we need the first element.
1392 Indices.push_back(Builder->getInt32(0));
1393 for (ExtractValueInst::idx_iterator I = EV.idx_begin(), E = EV.idx_end();
1394 I != E; ++I)
1395 Indices.push_back(Builder->getInt32(*I));
1396
1397 // We need to insert these at the location of the old load, not at that of
1398 // the extractvalue.
1399 Builder->SetInsertPoint(L->getParent(), L);
Jay Foad0a2a60a2011-07-22 08:16:57 +00001400 Value *GEP = Builder->CreateInBoundsGEP(L->getPointerOperand(), Indices);
Frits van Bommel34ceb4d2010-11-29 21:56:20 +00001401 // Returning the load directly will cause the main loop to insert it in
1402 // the wrong spot, so use ReplaceInstUsesWith().
1403 return ReplaceInstUsesWith(EV, Builder->CreateLoad(GEP));
1404 }
1405 // We could simplify extracts from other values. Note that nested extracts may
1406 // already be simplified implicitly by the above: extract (extract (insert) )
Matthijs Kooijman780ae5e2008-07-16 12:55:45 +00001407 // will be translated into extract ( insert ( extract ) ) first and then just
Frits van Bommel34ceb4d2010-11-29 21:56:20 +00001408 // the value inserted, if appropriate. Similarly for extracts from single-use
1409 // loads: extract (extract (load)) will be translated to extract (load (gep))
1410 // and if again single-use then via load (gep (gep)) to load (gep).
1411 // However, double extracts from e.g. function arguments or return values
1412 // aren't handled yet.
Matthijs Kooijmana9012ec2008-06-11 14:05:05 +00001413 return 0;
1414}
1415
Duncan Sands0ad7b6e2011-09-30 13:12:16 +00001416enum Personality_Type {
1417 Unknown_Personality,
1418 GNU_Ada_Personality,
Bill Wendling76f267d2011-10-17 21:20:24 +00001419 GNU_CXX_Personality,
1420 GNU_ObjC_Personality
Duncan Sands0ad7b6e2011-09-30 13:12:16 +00001421};
1422
1423/// RecognizePersonality - See if the given exception handling personality
1424/// function is one that we understand. If so, return a description of it;
1425/// otherwise return Unknown_Personality.
1426static Personality_Type RecognizePersonality(Value *Pers) {
1427 Function *F = dyn_cast<Function>(Pers->stripPointerCasts());
1428 if (!F)
1429 return Unknown_Personality;
1430 return StringSwitch<Personality_Type>(F->getName())
1431 .Case("__gnat_eh_personality", GNU_Ada_Personality)
Bill Wendling76f267d2011-10-17 21:20:24 +00001432 .Case("__gxx_personality_v0", GNU_CXX_Personality)
1433 .Case("__objc_personality_v0", GNU_ObjC_Personality)
Duncan Sands0ad7b6e2011-09-30 13:12:16 +00001434 .Default(Unknown_Personality);
1435}
1436
1437/// isCatchAll - Return 'true' if the given typeinfo will match anything.
1438static bool isCatchAll(Personality_Type Personality, Constant *TypeInfo) {
1439 switch (Personality) {
1440 case Unknown_Personality:
1441 return false;
1442 case GNU_Ada_Personality:
1443 // While __gnat_all_others_value will match any Ada exception, it doesn't
1444 // match foreign exceptions (or didn't, before gcc-4.7).
1445 return false;
1446 case GNU_CXX_Personality:
Bill Wendling76f267d2011-10-17 21:20:24 +00001447 case GNU_ObjC_Personality:
Duncan Sands0ad7b6e2011-09-30 13:12:16 +00001448 return TypeInfo->isNullValue();
1449 }
1450 llvm_unreachable("Unknown personality!");
1451}
1452
1453static bool shorter_filter(const Value *LHS, const Value *RHS) {
1454 return
1455 cast<ArrayType>(LHS->getType())->getNumElements()
1456 <
1457 cast<ArrayType>(RHS->getType())->getNumElements();
1458}
1459
1460Instruction *InstCombiner::visitLandingPadInst(LandingPadInst &LI) {
1461 // The logic here should be correct for any real-world personality function.
1462 // However if that turns out not to be true, the offending logic can always
1463 // be conditioned on the personality function, like the catch-all logic is.
1464 Personality_Type Personality = RecognizePersonality(LI.getPersonalityFn());
1465
1466 // Simplify the list of clauses, eg by removing repeated catch clauses
1467 // (these are often created by inlining).
1468 bool MakeNewInstruction = false; // If true, recreate using the following:
1469 SmallVector<Value *, 16> NewClauses; // - Clauses for the new instruction;
1470 bool CleanupFlag = LI.isCleanup(); // - The new instruction is a cleanup.
1471
1472 SmallPtrSet<Value *, 16> AlreadyCaught; // Typeinfos known caught already.
1473 for (unsigned i = 0, e = LI.getNumClauses(); i != e; ++i) {
1474 bool isLastClause = i + 1 == e;
1475 if (LI.isCatch(i)) {
1476 // A catch clause.
1477 Value *CatchClause = LI.getClause(i);
1478 Constant *TypeInfo = cast<Constant>(CatchClause->stripPointerCasts());
1479
1480 // If we already saw this clause, there is no point in having a second
1481 // copy of it.
1482 if (AlreadyCaught.insert(TypeInfo)) {
1483 // This catch clause was not already seen.
1484 NewClauses.push_back(CatchClause);
1485 } else {
1486 // Repeated catch clause - drop the redundant copy.
1487 MakeNewInstruction = true;
1488 }
1489
1490 // If this is a catch-all then there is no point in keeping any following
1491 // clauses or marking the landingpad as having a cleanup.
1492 if (isCatchAll(Personality, TypeInfo)) {
1493 if (!isLastClause)
1494 MakeNewInstruction = true;
1495 CleanupFlag = false;
1496 break;
1497 }
1498 } else {
1499 // A filter clause. If any of the filter elements were already caught
1500 // then they can be dropped from the filter. It is tempting to try to
1501 // exploit the filter further by saying that any typeinfo that does not
1502 // occur in the filter can't be caught later (and thus can be dropped).
1503 // However this would be wrong, since typeinfos can match without being
1504 // equal (for example if one represents a C++ class, and the other some
1505 // class derived from it).
1506 assert(LI.isFilter(i) && "Unsupported landingpad clause!");
1507 Value *FilterClause = LI.getClause(i);
1508 ArrayType *FilterType = cast<ArrayType>(FilterClause->getType());
1509 unsigned NumTypeInfos = FilterType->getNumElements();
1510
1511 // An empty filter catches everything, so there is no point in keeping any
1512 // following clauses or marking the landingpad as having a cleanup. By
1513 // dealing with this case here the following code is made a bit simpler.
1514 if (!NumTypeInfos) {
1515 NewClauses.push_back(FilterClause);
1516 if (!isLastClause)
1517 MakeNewInstruction = true;
1518 CleanupFlag = false;
1519 break;
1520 }
1521
1522 bool MakeNewFilter = false; // If true, make a new filter.
1523 SmallVector<Constant *, 16> NewFilterElts; // New elements.
1524 if (isa<ConstantAggregateZero>(FilterClause)) {
1525 // Not an empty filter - it contains at least one null typeinfo.
1526 assert(NumTypeInfos > 0 && "Should have handled empty filter already!");
1527 Constant *TypeInfo =
1528 Constant::getNullValue(FilterType->getElementType());
1529 // If this typeinfo is a catch-all then the filter can never match.
1530 if (isCatchAll(Personality, TypeInfo)) {
1531 // Throw the filter away.
1532 MakeNewInstruction = true;
1533 continue;
1534 }
1535
1536 // There is no point in having multiple copies of this typeinfo, so
1537 // discard all but the first copy if there is more than one.
1538 NewFilterElts.push_back(TypeInfo);
1539 if (NumTypeInfos > 1)
1540 MakeNewFilter = true;
1541 } else {
1542 ConstantArray *Filter = cast<ConstantArray>(FilterClause);
1543 SmallPtrSet<Value *, 16> SeenInFilter; // For uniquing the elements.
1544 NewFilterElts.reserve(NumTypeInfos);
1545
1546 // Remove any filter elements that were already caught or that already
1547 // occurred in the filter. While there, see if any of the elements are
1548 // catch-alls. If so, the filter can be discarded.
1549 bool SawCatchAll = false;
1550 for (unsigned j = 0; j != NumTypeInfos; ++j) {
1551 Value *Elt = Filter->getOperand(j);
1552 Constant *TypeInfo = cast<Constant>(Elt->stripPointerCasts());
1553 if (isCatchAll(Personality, TypeInfo)) {
1554 // This element is a catch-all. Bail out, noting this fact.
1555 SawCatchAll = true;
1556 break;
1557 }
1558 if (AlreadyCaught.count(TypeInfo))
1559 // Already caught by an earlier clause, so having it in the filter
1560 // is pointless.
1561 continue;
1562 // There is no point in having multiple copies of the same typeinfo in
1563 // a filter, so only add it if we didn't already.
1564 if (SeenInFilter.insert(TypeInfo))
1565 NewFilterElts.push_back(cast<Constant>(Elt));
1566 }
1567 // A filter containing a catch-all cannot match anything by definition.
1568 if (SawCatchAll) {
1569 // Throw the filter away.
1570 MakeNewInstruction = true;
1571 continue;
1572 }
1573
1574 // If we dropped something from the filter, make a new one.
1575 if (NewFilterElts.size() < NumTypeInfos)
1576 MakeNewFilter = true;
1577 }
1578 if (MakeNewFilter) {
1579 FilterType = ArrayType::get(FilterType->getElementType(),
1580 NewFilterElts.size());
1581 FilterClause = ConstantArray::get(FilterType, NewFilterElts);
1582 MakeNewInstruction = true;
1583 }
1584
1585 NewClauses.push_back(FilterClause);
1586
1587 // If the new filter is empty then it will catch everything so there is
1588 // no point in keeping any following clauses or marking the landingpad
1589 // as having a cleanup. The case of the original filter being empty was
1590 // already handled above.
1591 if (MakeNewFilter && !NewFilterElts.size()) {
1592 assert(MakeNewInstruction && "New filter but not a new instruction!");
1593 CleanupFlag = false;
1594 break;
1595 }
1596 }
1597 }
1598
1599 // If several filters occur in a row then reorder them so that the shortest
1600 // filters come first (those with the smallest number of elements). This is
1601 // advantageous because shorter filters are more likely to match, speeding up
1602 // unwinding, but mostly because it increases the effectiveness of the other
1603 // filter optimizations below.
1604 for (unsigned i = 0, e = NewClauses.size(); i + 1 < e; ) {
1605 unsigned j;
1606 // Find the maximal 'j' s.t. the range [i, j) consists entirely of filters.
1607 for (j = i; j != e; ++j)
1608 if (!isa<ArrayType>(NewClauses[j]->getType()))
1609 break;
1610
1611 // Check whether the filters are already sorted by length. We need to know
1612 // if sorting them is actually going to do anything so that we only make a
1613 // new landingpad instruction if it does.
1614 for (unsigned k = i; k + 1 < j; ++k)
1615 if (shorter_filter(NewClauses[k+1], NewClauses[k])) {
1616 // Not sorted, so sort the filters now. Doing an unstable sort would be
1617 // correct too but reordering filters pointlessly might confuse users.
1618 std::stable_sort(NewClauses.begin() + i, NewClauses.begin() + j,
1619 shorter_filter);
1620 MakeNewInstruction = true;
1621 break;
1622 }
1623
1624 // Look for the next batch of filters.
1625 i = j + 1;
1626 }
1627
1628 // If typeinfos matched if and only if equal, then the elements of a filter L
1629 // that occurs later than a filter F could be replaced by the intersection of
1630 // the elements of F and L. In reality two typeinfos can match without being
1631 // equal (for example if one represents a C++ class, and the other some class
1632 // derived from it) so it would be wrong to perform this transform in general.
1633 // However the transform is correct and useful if F is a subset of L. In that
1634 // case L can be replaced by F, and thus removed altogether since repeating a
1635 // filter is pointless. So here we look at all pairs of filters F and L where
1636 // L follows F in the list of clauses, and remove L if every element of F is
1637 // an element of L. This can occur when inlining C++ functions with exception
1638 // specifications.
1639 for (unsigned i = 0; i + 1 < NewClauses.size(); ++i) {
1640 // Examine each filter in turn.
1641 Value *Filter = NewClauses[i];
1642 ArrayType *FTy = dyn_cast<ArrayType>(Filter->getType());
1643 if (!FTy)
1644 // Not a filter - skip it.
1645 continue;
1646 unsigned FElts = FTy->getNumElements();
1647 // Examine each filter following this one. Doing this backwards means that
1648 // we don't have to worry about filters disappearing under us when removed.
1649 for (unsigned j = NewClauses.size() - 1; j != i; --j) {
1650 Value *LFilter = NewClauses[j];
1651 ArrayType *LTy = dyn_cast<ArrayType>(LFilter->getType());
1652 if (!LTy)
1653 // Not a filter - skip it.
1654 continue;
1655 // If Filter is a subset of LFilter, i.e. every element of Filter is also
1656 // an element of LFilter, then discard LFilter.
1657 SmallVector<Value *, 16>::iterator J = NewClauses.begin() + j;
1658 // If Filter is empty then it is a subset of LFilter.
1659 if (!FElts) {
1660 // Discard LFilter.
1661 NewClauses.erase(J);
1662 MakeNewInstruction = true;
1663 // Move on to the next filter.
1664 continue;
1665 }
1666 unsigned LElts = LTy->getNumElements();
1667 // If Filter is longer than LFilter then it cannot be a subset of it.
1668 if (FElts > LElts)
1669 // Move on to the next filter.
1670 continue;
1671 // At this point we know that LFilter has at least one element.
1672 if (isa<ConstantAggregateZero>(LFilter)) { // LFilter only contains zeros.
1673 // Filter is a subset of LFilter iff Filter contains only zeros (as we
1674 // already know that Filter is not longer than LFilter).
1675 if (isa<ConstantAggregateZero>(Filter)) {
1676 assert(FElts <= LElts && "Should have handled this case earlier!");
1677 // Discard LFilter.
1678 NewClauses.erase(J);
1679 MakeNewInstruction = true;
1680 }
1681 // Move on to the next filter.
1682 continue;
1683 }
1684 ConstantArray *LArray = cast<ConstantArray>(LFilter);
1685 if (isa<ConstantAggregateZero>(Filter)) { // Filter only contains zeros.
1686 // Since Filter is non-empty and contains only zeros, it is a subset of
1687 // LFilter iff LFilter contains a zero.
1688 assert(FElts > 0 && "Should have eliminated the empty filter earlier!");
1689 for (unsigned l = 0; l != LElts; ++l)
1690 if (LArray->getOperand(l)->isNullValue()) {
1691 // LFilter contains a zero - discard it.
1692 NewClauses.erase(J);
1693 MakeNewInstruction = true;
1694 break;
1695 }
1696 // Move on to the next filter.
1697 continue;
1698 }
1699 // At this point we know that both filters are ConstantArrays. Loop over
1700 // operands to see whether every element of Filter is also an element of
1701 // LFilter. Since filters tend to be short this is probably faster than
1702 // using a method that scales nicely.
1703 ConstantArray *FArray = cast<ConstantArray>(Filter);
1704 bool AllFound = true;
1705 for (unsigned f = 0; f != FElts; ++f) {
1706 Value *FTypeInfo = FArray->getOperand(f)->stripPointerCasts();
1707 AllFound = false;
1708 for (unsigned l = 0; l != LElts; ++l) {
1709 Value *LTypeInfo = LArray->getOperand(l)->stripPointerCasts();
1710 if (LTypeInfo == FTypeInfo) {
1711 AllFound = true;
1712 break;
1713 }
1714 }
1715 if (!AllFound)
1716 break;
1717 }
1718 if (AllFound) {
1719 // Discard LFilter.
1720 NewClauses.erase(J);
1721 MakeNewInstruction = true;
1722 }
1723 // Move on to the next filter.
1724 }
1725 }
1726
1727 // If we changed any of the clauses, replace the old landingpad instruction
1728 // with a new one.
1729 if (MakeNewInstruction) {
1730 LandingPadInst *NLI = LandingPadInst::Create(LI.getType(),
1731 LI.getPersonalityFn(),
1732 NewClauses.size());
1733 for (unsigned i = 0, e = NewClauses.size(); i != e; ++i)
1734 NLI->addClause(NewClauses[i]);
1735 // A landing pad with no clauses must have the cleanup flag set. It is
1736 // theoretically possible, though highly unlikely, that we eliminated all
1737 // clauses. If so, force the cleanup flag to true.
1738 if (NewClauses.empty())
1739 CleanupFlag = true;
1740 NLI->setCleanup(CleanupFlag);
1741 return NLI;
1742 }
1743
1744 // Even if none of the clauses changed, we may nonetheless have understood
1745 // that the cleanup flag is pointless. Clear it if so.
1746 if (LI.isCleanup() != CleanupFlag) {
1747 assert(!CleanupFlag && "Adding a cleanup, not removing one?!");
1748 LI.setCleanup(CleanupFlag);
1749 return &LI;
1750 }
1751
1752 return 0;
1753}
1754
Chris Lattnera844fc4c2006-04-10 22:45:52 +00001755
Robert Bocchino1d7456d2006-01-13 22:48:06 +00001756
Chris Lattnerea1c4542004-12-08 23:43:58 +00001757
1758/// TryToSinkInstruction - Try to move the specified instruction from its
1759/// current block into the beginning of DestBlock, which can only happen if it's
1760/// safe to move the instruction past all of the instructions between it and the
1761/// end of its block.
1762static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
1763 assert(I->hasOneUse() && "Invariants didn't hold!");
1764
Bill Wendling9d6070f2011-08-15 21:14:31 +00001765 // Cannot move control-flow-involving, volatile loads, vaarg, etc.
Bill Wendlingc9b2a982011-08-17 20:36:44 +00001766 if (isa<PHINode>(I) || isa<LandingPadInst>(I) || I->mayHaveSideEffects() ||
1767 isa<TerminatorInst>(I))
Chris Lattnerbfc538c2008-05-09 15:07:33 +00001768 return false;
Misha Brukmanfd939082005-04-21 23:48:37 +00001769
Chris Lattnerea1c4542004-12-08 23:43:58 +00001770 // Do not sink alloca instructions out of the entry block.
Dan Gohmanecb7a772007-03-22 16:38:57 +00001771 if (isa<AllocaInst>(I) && I->getParent() ==
1772 &DestBlock->getParent()->getEntryBlock())
Chris Lattnerea1c4542004-12-08 23:43:58 +00001773 return false;
1774
Chris Lattner96a52a62004-12-09 07:14:34 +00001775 // We can only sink load instructions if there is nothing between the load and
1776 // the end of block that could change the value.
Chris Lattner2539e332008-05-08 17:37:37 +00001777 if (I->mayReadFromMemory()) {
1778 for (BasicBlock::iterator Scan = I, E = I->getParent()->end();
Chris Lattner96a52a62004-12-09 07:14:34 +00001779 Scan != E; ++Scan)
1780 if (Scan->mayWriteToMemory())
1781 return false;
Chris Lattner96a52a62004-12-09 07:14:34 +00001782 }
Chris Lattnerea1c4542004-12-08 23:43:58 +00001783
Bill Wendling5b6f42f2011-08-16 20:45:24 +00001784 BasicBlock::iterator InsertPos = DestBlock->getFirstInsertionPt();
Chris Lattner4bc5f802005-08-08 19:11:57 +00001785 I->moveBefore(InsertPos);
Chris Lattnerea1c4542004-12-08 23:43:58 +00001786 ++NumSunkInst;
1787 return true;
1788}
1789
Chris Lattnerf4f5a772006-05-10 19:00:36 +00001790
1791/// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
1792/// all reachable code to the worklist.
1793///
1794/// This has a couple of tricks to make the code faster and more powerful. In
1795/// particular, we constant fold and DCE instructions as we go, to avoid adding
1796/// them to the worklist (this significantly speeds up instcombine on code where
1797/// many instructions are dead or constant). Additionally, if we find a branch
1798/// whose condition is a known constant, we only visit the reachable successors.
1799///
Chris Lattner2ee743b2009-10-15 04:59:28 +00001800static bool AddReachableCodeToWorklist(BasicBlock *BB,
Chris Lattner1f87a582007-02-15 19:41:52 +00001801 SmallPtrSet<BasicBlock*, 64> &Visited,
Chris Lattnerdbab3862007-03-02 21:28:56 +00001802 InstCombiner &IC,
Chris Lattner8c8c66a2006-05-11 17:11:52 +00001803 const TargetData *TD) {
Chris Lattner2ee743b2009-10-15 04:59:28 +00001804 bool MadeIRChange = false;
Chris Lattner2806dff2008-08-15 04:03:01 +00001805 SmallVector<BasicBlock*, 256> Worklist;
Chris Lattner2c7718a2007-03-23 19:17:18 +00001806 Worklist.push_back(BB);
Chris Lattnerf4f5a772006-05-10 19:00:36 +00001807
Benjamin Kramera53fe602010-10-23 17:10:24 +00001808 SmallVector<Instruction*, 128> InstrsForInstCombineWorklist;
Eli Friedmana4d4aeb2011-05-24 18:52:07 +00001809 DenseMap<ConstantExpr*, Constant*> FoldedConstants;
1810
Dan Gohman321a8132010-01-05 16:27:25 +00001811 do {
1812 BB = Worklist.pop_back_val();
Chris Lattner2c7718a2007-03-23 19:17:18 +00001813
1814 // We have now visited this block! If we've already been here, ignore it.
1815 if (!Visited.insert(BB)) continue;
Devang Patel7fe1dec2008-11-19 18:56:50 +00001816
Chris Lattner2c7718a2007-03-23 19:17:18 +00001817 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
1818 Instruction *Inst = BBI++;
Chris Lattnerf4f5a772006-05-10 19:00:36 +00001819
Chris Lattner2c7718a2007-03-23 19:17:18 +00001820 // DCE instruction if trivially dead.
1821 if (isInstructionTriviallyDead(Inst)) {
1822 ++NumDeadInst;
Chris Lattnerbdff5482009-08-23 04:37:46 +00001823 DEBUG(errs() << "IC: DCE: " << *Inst << '\n');
Chris Lattner2c7718a2007-03-23 19:17:18 +00001824 Inst->eraseFromParent();
1825 continue;
1826 }
1827
1828 // ConstantProp instruction if trivially constant.
Chris Lattnere2cc1ad2009-10-15 04:13:44 +00001829 if (!Inst->use_empty() && isa<Constant>(Inst->getOperand(0)))
Chris Lattner7b550cc2009-11-06 04:27:31 +00001830 if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
Chris Lattnere2cc1ad2009-10-15 04:13:44 +00001831 DEBUG(errs() << "IC: ConstFold to: " << *C << " from: "
1832 << *Inst << '\n');
1833 Inst->replaceAllUsesWith(C);
1834 ++NumConstProp;
1835 Inst->eraseFromParent();
1836 continue;
1837 }
Chris Lattner2ee743b2009-10-15 04:59:28 +00001838
Chris Lattner2ee743b2009-10-15 04:59:28 +00001839 if (TD) {
1840 // See if we can constant fold its operands.
1841 for (User::op_iterator i = Inst->op_begin(), e = Inst->op_end();
1842 i != e; ++i) {
1843 ConstantExpr *CE = dyn_cast<ConstantExpr>(i);
1844 if (CE == 0) continue;
Eli Friedmana4d4aeb2011-05-24 18:52:07 +00001845
1846 Constant*& FoldRes = FoldedConstants[CE];
1847 if (!FoldRes)
1848 FoldRes = ConstantFoldConstantExpression(CE, TD);
1849 if (!FoldRes)
1850 FoldRes = CE;
1851
1852 if (FoldRes != CE) {
1853 *i = FoldRes;
Chris Lattner2ee743b2009-10-15 04:59:28 +00001854 MadeIRChange = true;
1855 }
1856 }
1857 }
Devang Patel7fe1dec2008-11-19 18:56:50 +00001858
Chris Lattner67f7d542009-10-12 03:58:40 +00001859 InstrsForInstCombineWorklist.push_back(Inst);
Chris Lattnerf4f5a772006-05-10 19:00:36 +00001860 }
Chris Lattner2c7718a2007-03-23 19:17:18 +00001861
1862 // Recursively visit successors. If this is a branch or switch on a
1863 // constant, only visit the reachable successor.
1864 TerminatorInst *TI = BB->getTerminator();
1865 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1866 if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
1867 bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
Nick Lewycky91436992008-03-09 08:50:23 +00001868 BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
Nick Lewycky280a6e62008-04-25 16:53:59 +00001869 Worklist.push_back(ReachableBB);
Chris Lattner2c7718a2007-03-23 19:17:18 +00001870 continue;
1871 }
1872 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1873 if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
1874 // See if this is an explicit destination.
1875 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
1876 if (SI->getCaseValue(i) == Cond) {
Nick Lewycky91436992008-03-09 08:50:23 +00001877 BasicBlock *ReachableBB = SI->getSuccessor(i);
Nick Lewycky280a6e62008-04-25 16:53:59 +00001878 Worklist.push_back(ReachableBB);
Chris Lattner2c7718a2007-03-23 19:17:18 +00001879 continue;
1880 }
1881
1882 // Otherwise it is the default destination.
1883 Worklist.push_back(SI->getSuccessor(0));
1884 continue;
1885 }
1886 }
1887
1888 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
1889 Worklist.push_back(TI->getSuccessor(i));
Dan Gohman321a8132010-01-05 16:27:25 +00001890 } while (!Worklist.empty());
Chris Lattner67f7d542009-10-12 03:58:40 +00001891
1892 // Once we've found all of the instructions to add to instcombine's worklist,
1893 // add them in reverse order. This way instcombine will visit from the top
1894 // of the function down. This jives well with the way that it adds all uses
1895 // of instructions to the worklist after doing a transformation, thus avoiding
1896 // some N^2 behavior in pathological cases.
1897 IC.Worklist.AddInitialGroup(&InstrsForInstCombineWorklist[0],
1898 InstrsForInstCombineWorklist.size());
Chris Lattner2ee743b2009-10-15 04:59:28 +00001899
1900 return MadeIRChange;
Chris Lattnerf4f5a772006-05-10 19:00:36 +00001901}
1902
Chris Lattnerec9c3582007-03-03 02:04:50 +00001903bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
Chris Lattnerb0b822c2009-08-31 06:57:37 +00001904 MadeIRChange = false;
Chris Lattnerec9c3582007-03-03 02:04:50 +00001905
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001906 DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
Benjamin Kramera7b0cb72011-11-15 16:27:03 +00001907 << F.getName() << "\n");
Chris Lattner8a2a3112001-12-14 16:52:21 +00001908
Chris Lattnerb3d59702005-07-07 20:40:38 +00001909 {
Chris Lattnerf4f5a772006-05-10 19:00:36 +00001910 // Do a depth-first traversal of the function, populate the worklist with
1911 // the reachable instructions. Ignore blocks that are not reachable. Keep
1912 // track of which blocks we visit.
Chris Lattner1f87a582007-02-15 19:41:52 +00001913 SmallPtrSet<BasicBlock*, 64> Visited;
Chris Lattner2ee743b2009-10-15 04:59:28 +00001914 MadeIRChange |= AddReachableCodeToWorklist(F.begin(), Visited, *this, TD);
Jeff Cohen00b168892005-07-27 06:12:32 +00001915
Chris Lattnerb3d59702005-07-07 20:40:38 +00001916 // Do a quick scan over the function. If we find any blocks that are
1917 // unreachable, remove any instructions inside of them. This prevents
1918 // the instcombine code from having to deal with some bad special cases.
Bill Wendling6bb4e7e2011-09-01 21:29:49 +00001919 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1920 if (Visited.count(BB)) continue;
1921
Bill Wendlinga2684682011-09-04 09:43:36 +00001922 // Delete the instructions backwards, as it has a reduced likelihood of
1923 // having to update as many def-use and use-def chains.
1924 Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
1925 while (EndInst != BB->begin()) {
1926 // Delete the next to last instruction.
1927 BasicBlock::iterator I = EndInst;
1928 Instruction *Inst = --I;
Bill Wendling6bb4e7e2011-09-01 21:29:49 +00001929 if (!Inst->use_empty())
1930 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
Bill Wendlinga2684682011-09-04 09:43:36 +00001931 if (isa<LandingPadInst>(Inst)) {
1932 EndInst = Inst;
Bill Wendling6bb4e7e2011-09-01 21:29:49 +00001933 continue;
Bill Wendlinga2684682011-09-04 09:43:36 +00001934 }
Bill Wendling6bb4e7e2011-09-01 21:29:49 +00001935 if (!isa<DbgInfoIntrinsic>(Inst)) {
1936 ++NumDeadInst;
1937 MadeIRChange = true;
Chris Lattnerb3d59702005-07-07 20:40:38 +00001938 }
Bill Wendling6bb4e7e2011-09-01 21:29:49 +00001939 Inst->eraseFromParent();
Chris Lattnerb3d59702005-07-07 20:40:38 +00001940 }
Bill Wendling6bb4e7e2011-09-01 21:29:49 +00001941 }
Chris Lattnerb3d59702005-07-07 20:40:38 +00001942 }
Chris Lattner8a2a3112001-12-14 16:52:21 +00001943
Chris Lattner873ff012009-08-30 05:55:36 +00001944 while (!Worklist.isEmpty()) {
1945 Instruction *I = Worklist.RemoveOne();
Chris Lattnerdbab3862007-03-02 21:28:56 +00001946 if (I == 0) continue; // skip null values.
Chris Lattner8a2a3112001-12-14 16:52:21 +00001947
Chris Lattner8c8c66a2006-05-11 17:11:52 +00001948 // Check to see if we can DCE the instruction.
Chris Lattner62b14df2002-09-02 04:59:56 +00001949 if (isInstructionTriviallyDead(I)) {
Chris Lattnerbdff5482009-08-23 04:37:46 +00001950 DEBUG(errs() << "IC: DCE: " << *I << '\n');
Chris Lattner7a1e9242009-08-30 06:13:40 +00001951 EraseInstFromFunction(*I);
1952 ++NumDeadInst;
Chris Lattnerb0b822c2009-08-31 06:57:37 +00001953 MadeIRChange = true;
Chris Lattner4bb7c022003-10-06 17:11:01 +00001954 continue;
1955 }
Chris Lattner62b14df2002-09-02 04:59:56 +00001956
Chris Lattner8c8c66a2006-05-11 17:11:52 +00001957 // Instruction isn't dead, see if we can constant propagate it.
Chris Lattnere2cc1ad2009-10-15 04:13:44 +00001958 if (!I->use_empty() && isa<Constant>(I->getOperand(0)))
Chris Lattner7b550cc2009-11-06 04:27:31 +00001959 if (Constant *C = ConstantFoldInstruction(I, TD)) {
Chris Lattnere2cc1ad2009-10-15 04:13:44 +00001960 DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');
Chris Lattnerad5fec12005-01-28 19:32:01 +00001961
Chris Lattnere2cc1ad2009-10-15 04:13:44 +00001962 // Add operands to the worklist.
1963 ReplaceInstUsesWith(*I, C);
1964 ++NumConstProp;
1965 EraseInstFromFunction(*I);
1966 MadeIRChange = true;
1967 continue;
1968 }
Chris Lattner4bb7c022003-10-06 17:11:01 +00001969
Chris Lattnerea1c4542004-12-08 23:43:58 +00001970 // See if we can trivially sink this instruction to a successor basic block.
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001971 if (I->hasOneUse()) {
Chris Lattnerea1c4542004-12-08 23:43:58 +00001972 BasicBlock *BB = I->getParent();
Chris Lattner8db2cd12009-10-14 15:21:58 +00001973 Instruction *UserInst = cast<Instruction>(I->use_back());
1974 BasicBlock *UserParent;
1975
1976 // Get the block the use occurs in.
1977 if (PHINode *PN = dyn_cast<PHINode>(UserInst))
1978 UserParent = PN->getIncomingBlock(I->use_begin().getUse());
1979 else
1980 UserParent = UserInst->getParent();
1981
Chris Lattnerea1c4542004-12-08 23:43:58 +00001982 if (UserParent != BB) {
1983 bool UserIsSuccessor = false;
1984 // See if the user is one of our successors.
1985 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
1986 if (*SI == UserParent) {
1987 UserIsSuccessor = true;
1988 break;
1989 }
1990
1991 // If the user is one of our immediate successors, and if that successor
1992 // only has us as a predecessors (we'd have to split the critical edge
1993 // otherwise), we can keep going.
Chris Lattner8db2cd12009-10-14 15:21:58 +00001994 if (UserIsSuccessor && UserParent->getSinglePredecessor())
Chris Lattnerea1c4542004-12-08 23:43:58 +00001995 // Okay, the CFG is simple enough, try to sink this instruction.
Chris Lattnerb0b822c2009-08-31 06:57:37 +00001996 MadeIRChange |= TryToSinkInstruction(I, UserParent);
Chris Lattnerea1c4542004-12-08 23:43:58 +00001997 }
1998 }
1999
Chris Lattner74381062009-08-30 07:44:24 +00002000 // Now that we have an instruction, try combining it to simplify it.
2001 Builder->SetInsertPoint(I->getParent(), I);
Eli Friedmanef819d02011-05-18 01:28:27 +00002002 Builder->SetCurrentDebugLocation(I->getDebugLoc());
Chris Lattner74381062009-08-30 07:44:24 +00002003
Reid Spencera9b81012007-03-26 17:44:01 +00002004#ifndef NDEBUG
2005 std::string OrigI;
2006#endif
Chris Lattnerbdff5482009-08-23 04:37:46 +00002007 DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str(););
Jeffrey Yasskin43069632009-10-08 00:12:24 +00002008 DEBUG(errs() << "IC: Visiting: " << OrigI << '\n');
2009
Chris Lattner90ac28c2002-08-02 19:29:35 +00002010 if (Instruction *Result = visit(*I)) {
Chris Lattner3dec1f22002-05-10 15:38:35 +00002011 ++NumCombined;
Chris Lattnerdd841ae2002-04-18 17:39:14 +00002012 // Should we replace the old instruction with a new one?
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +00002013 if (Result != I) {
Jim Grosbache2999b42011-10-05 20:44:29 +00002014 DEBUG(errs() << "IC: Old = " << *I << '\n'
2015 << " New = " << *Result << '\n');
2016
Eli Friedmana311c342011-05-27 00:19:40 +00002017 if (!I->getDebugLoc().isUnknown())
2018 Result->setDebugLoc(I->getDebugLoc());
Chris Lattnerf523d062004-06-09 05:08:07 +00002019 // Everything uses the new instruction now.
2020 I->replaceAllUsesWith(Result);
2021
Jim Grosbach35d9da32011-10-05 20:53:43 +00002022 // Move the name to the new instruction first.
2023 Result->takeName(I);
2024
Jim Grosbache2999b42011-10-05 20:44:29 +00002025 // Push the new instruction and any users onto the worklist.
2026 Worklist.Add(Result);
2027 Worklist.AddUsersToWorkList(*Result);
2028
Chris Lattner4bb7c022003-10-06 17:11:01 +00002029 // Insert the new instruction into the basic block...
2030 BasicBlock *InstParent = I->getParent();
Chris Lattnerbac32862004-11-14 19:13:23 +00002031 BasicBlock::iterator InsertPos = I;
2032
Eli Friedman049260d2011-11-01 04:49:29 +00002033 // If we replace a PHI with something that isn't a PHI, fix up the
2034 // insertion point.
2035 if (!isa<PHINode>(Result) && isa<PHINode>(InsertPos))
2036 InsertPos = InstParent->getFirstInsertionPt();
Chris Lattnerbac32862004-11-14 19:13:23 +00002037
2038 InstParent->getInstList().insert(InsertPos, Result);
Chris Lattner4bb7c022003-10-06 17:11:01 +00002039
Chris Lattner7a1e9242009-08-30 06:13:40 +00002040 EraseInstFromFunction(*I);
Chris Lattner7e708292002-06-25 16:13:24 +00002041 } else {
Evan Chengc7baf682007-03-27 16:44:48 +00002042#ifndef NDEBUG
Chris Lattnerbdff5482009-08-23 04:37:46 +00002043 DEBUG(errs() << "IC: Mod = " << OrigI << '\n'
2044 << " New = " << *I << '\n');
Evan Chengc7baf682007-03-27 16:44:48 +00002045#endif
Chris Lattner0cea42a2004-03-13 23:54:27 +00002046
Chris Lattner90ac28c2002-08-02 19:29:35 +00002047 // If the instruction was modified, it's possible that it is now dead.
2048 // if so, remove it.
Chris Lattner00d51312004-05-01 23:27:23 +00002049 if (isInstructionTriviallyDead(I)) {
Chris Lattner7a1e9242009-08-30 06:13:40 +00002050 EraseInstFromFunction(*I);
Chris Lattnerf523d062004-06-09 05:08:07 +00002051 } else {
Chris Lattner7a1e9242009-08-30 06:13:40 +00002052 Worklist.Add(I);
Chris Lattnere5ecdb52009-08-30 06:22:51 +00002053 Worklist.AddUsersToWorkList(*I);
Chris Lattner90ac28c2002-08-02 19:29:35 +00002054 }
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +00002055 }
Chris Lattnerb0b822c2009-08-31 06:57:37 +00002056 MadeIRChange = true;
Chris Lattner8a2a3112001-12-14 16:52:21 +00002057 }
2058 }
2059
Chris Lattner873ff012009-08-30 05:55:36 +00002060 Worklist.Zap();
Chris Lattnerb0b822c2009-08-31 06:57:37 +00002061 return MadeIRChange;
Chris Lattnerbd0ef772002-02-26 21:46:54 +00002062}
2063
Chris Lattnerec9c3582007-03-03 02:04:50 +00002064
2065bool InstCombiner::runOnFunction(Function &F) {
Chris Lattnere2cc1ad2009-10-15 04:13:44 +00002066 TD = getAnalysisIfAvailable<TargetData>();
2067
Chris Lattner74381062009-08-30 07:44:24 +00002068
2069 /// Builder - This is an IRBuilder that automatically inserts new
2070 /// instructions into the worklist when they are created.
Chris Lattnere2cc1ad2009-10-15 04:13:44 +00002071 IRBuilder<true, TargetFolder, InstCombineIRInserter>
Chris Lattnerf55eeb92009-11-06 05:59:53 +00002072 TheBuilder(F.getContext(), TargetFolder(TD),
Chris Lattner74381062009-08-30 07:44:24 +00002073 InstCombineIRInserter(Worklist));
2074 Builder = &TheBuilder;
2075
Chris Lattnerec9c3582007-03-03 02:04:50 +00002076 bool EverMadeChange = false;
2077
Devang Patel813c9a02011-03-17 22:18:16 +00002078 // Lower dbg.declare intrinsics otherwise their value may be clobbered
2079 // by instcombiner.
2080 EverMadeChange = LowerDbgDeclare(F);
2081
Chris Lattnerec9c3582007-03-03 02:04:50 +00002082 // Iterate while there is work to do.
2083 unsigned Iteration = 0;
Bill Wendlinga6c31122008-05-14 22:45:20 +00002084 while (DoOneIteration(F, Iteration++))
Chris Lattnerec9c3582007-03-03 02:04:50 +00002085 EverMadeChange = true;
Chris Lattner74381062009-08-30 07:44:24 +00002086
2087 Builder = 0;
Chris Lattnerec9c3582007-03-03 02:04:50 +00002088 return EverMadeChange;
2089}
2090
Brian Gaeke96d4bf72004-07-27 17:43:21 +00002091FunctionPass *llvm::createInstructionCombiningPass() {
Chris Lattnerdd841ae2002-04-18 17:39:14 +00002092 return new InstCombiner();
Chris Lattnerbd0ef772002-02-26 21:46:54 +00002093}