blob: 9fc9ef0a0d77d0c6794e3c8ae5a65a1a27da842b [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
65 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
66 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
69 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
70 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
90 Use *OL = OperandList;
91 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000092 OL[i] = PN.getOperand(i);
93 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000094 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000095 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000096}
97
Gordon Henriksen14a55692007-12-10 02:14:30 +000098PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +000099 if (OperandList)
100 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101}
102
103// removeIncomingValue - Remove an incoming value. This is useful if a
104// predecessor basic block is deleted.
105Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
106 unsigned NumOps = getNumOperands();
107 Use *OL = OperandList;
108 assert(Idx*2 < NumOps && "BB not in PHI node!");
109 Value *Removed = OL[Idx*2];
110
111 // Move everything after this operand down.
112 //
113 // FIXME: we could just swap with the end of the list, then erase. However,
114 // client might not expect this to happen. The code as it is thrashes the
115 // use/def lists, which is kinda lame.
116 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
117 OL[i-2] = OL[i];
118 OL[i-2+1] = OL[i+1];
119 }
120
121 // Nuke the last value.
122 OL[NumOps-2].set(0);
123 OL[NumOps-2+1].set(0);
124 NumOperands = NumOps-2;
125
126 // If the PHI node is dead, because it has zero entries, nuke it now.
127 if (NumOps == 2 && DeletePHIIfEmpty) {
128 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000129 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130 eraseFromParent();
131 }
132 return Removed;
133}
134
135/// resizeOperands - resize operands - This adjusts the length of the operands
136/// list according to the following behavior:
137/// 1. If NumOps == 0, grow the operand list in response to a push_back style
138/// of operation. This grows the number of ops by 1.5 times.
139/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
140/// 3. If NumOps == NumOperands, trim the reserved space.
141///
142void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000143 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000145 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000146 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
147 } else if (NumOps*2 > NumOperands) {
148 // No resize needed.
149 if (ReservedSpace >= NumOps) return;
150 } else if (NumOps == NumOperands) {
151 if (ReservedSpace == NumOps) return;
152 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000154 }
155
156 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000158 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000159 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000161 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
Nate Begemanb3923212005-08-04 23:24:19 +0000164/// hasConstantValue - If the specified PHI node always merges together the same
165/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000166Value *PHINode::hasConstantValue() const {
167 // Exploit the fact that phi nodes always have at least one entry.
168 Value *ConstantValue = getIncomingValue(0);
169 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
170 if (getIncomingValue(i) != ConstantValue)
171 return 0; // Incoming values not all the same.
172 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000173}
174
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000175
176//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000177// CallInst Implementation
178//===----------------------------------------------------------------------===//
179
Gordon Henriksen14a55692007-12-10 02:14:30 +0000180CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181}
182
Chris Lattner054ba2c2007-02-13 00:58:44 +0000183void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000184 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000185 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000186
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000188 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000189 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000190
Chris Lattner054ba2c2007-02-13 00:58:44 +0000191 assert((NumParams == FTy->getNumParams() ||
192 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000193 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000194 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000195 assert((i >= FTy->getNumParams() ||
196 FTy->getParamType(i) == Params[i]->getType()) &&
197 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000198 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000199 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000200}
201
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000203 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000204 Op<-1>() = Func;
205 Op<0>() = Actual1;
206 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000207
208 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000209 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000210 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000211
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000213 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000214 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000215 assert((0 >= FTy->getNumParams() ||
216 FTy->getParamType(0) == Actual1->getType()) &&
217 "Calling a function with a bad signature!");
218 assert((1 >= FTy->getNumParams() ||
219 FTy->getParamType(1) == Actual2->getType()) &&
220 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000221}
222
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000224 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000225 Op<-1>() = Func;
226 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000227
228 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000230 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000231
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232 assert((FTy->getNumParams() == 1 ||
233 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000235 assert((0 == FTy->getNumParams() ||
236 FTy->getParamType(0) == Actual->getType()) &&
237 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000238}
239
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000241 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000242 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000243
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000244 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000245 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000246 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000248 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000249}
250
Daniel Dunbar4975db62009-07-25 04:41:11 +0000251CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000252 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
254 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000255 Instruction::Call,
256 OperandTraits<CallInst>::op_end(this) - 2,
257 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000259 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260}
261
Daniel Dunbar4975db62009-07-25 04:41:11 +0000262CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263 BasicBlock *InsertAtEnd)
264 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
265 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000266 Instruction::Call,
267 OperandTraits<CallInst>::op_end(this) - 2,
268 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000270 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000272CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 Instruction *InsertBefore)
274 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
275 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000276 Instruction::Call,
277 OperandTraits<CallInst>::op_end(this) - 1,
278 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000280 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281}
282
Daniel Dunbar4975db62009-07-25 04:41:11 +0000283CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 BasicBlock *InsertAtEnd)
285 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
286 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000287 Instruction::Call,
288 OperandTraits<CallInst>::op_end(this) - 1,
289 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000291 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292}
293
Misha Brukmanb1c93172005-04-21 23:48:37 +0000294CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000295 : Instruction(CI.getType(), Instruction::Call,
296 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000297 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000298 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000299 setTailCall(CI.isTailCall());
300 setCallingConv(CI.getCallingConv());
301
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000302 Use *OL = OperandList;
303 Use *InOL = CI.OperandList;
304 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000305 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000306 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307}
308
Devang Patel4c758ea2008-09-25 21:00:45 +0000309void CallInst::addAttribute(unsigned i, Attributes attr) {
310 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000311 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000312 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000313}
314
Devang Patel4c758ea2008-09-25 21:00:45 +0000315void CallInst::removeAttribute(unsigned i, Attributes attr) {
316 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000317 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000318 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000319}
320
Devang Patelba3fa6c2008-09-23 23:03:40 +0000321bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000322 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000323 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000324 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000325 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000326 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000327}
328
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000329/// IsConstantOne - Return true only if val is constant int 1
330static bool IsConstantOne(Value *val) {
331 assert(val && "IsConstantOne does not work with NULL val");
332 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
333}
334
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000335static Instruction *createMalloc(Instruction *InsertBefore,
336 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000337 const Type *AllocTy, Value *AllocSize,
338 Value *ArraySize, Function *MallocF,
339 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000340 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000341 "createMalloc needs either InsertBefore or InsertAtEnd");
342
343 // malloc(type) becomes:
344 // bitcast (i8* malloc(typeSize)) to type*
345 // malloc(type, arraySize) becomes:
346 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000347 if (!ArraySize)
348 ArraySize = ConstantInt::get(IntPtrTy, 1);
349 else if (ArraySize->getType() != IntPtrTy) {
350 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000351 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
352 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000353 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000354 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
355 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000356 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000357
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000358 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000359 if (IsConstantOne(AllocSize)) {
360 AllocSize = ArraySize; // Operand * 1 = Operand
361 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
362 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
363 false /*ZExt*/);
364 // Malloc arg is constant product of type size and array size
365 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
366 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000367 // Multiply type size by the array size...
368 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000369 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
370 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000371 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000372 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
373 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000374 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000375 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000376
Victor Hernandez788eaab2009-09-18 19:20:02 +0000377 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378 // Create the call to Malloc.
379 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
380 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000381 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000382 Value *MallocFunc = MallocF;
383 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000384 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000385 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000386 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000387 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000388 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000389 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000390 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000391 Result = MCall;
392 if (Result->getType() != AllocPtrType)
393 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000394 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000395 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000396 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000397 Result = MCall;
398 if (Result->getType() != AllocPtrType) {
399 InsertAtEnd->getInstList().push_back(MCall);
400 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000401 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000402 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000403 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000404 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000405 if (Function *F = dyn_cast<Function>(MallocFunc)) {
406 MCall->setCallingConv(F->getCallingConv());
407 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
408 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000409 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000410
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000411 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000412}
413
414/// CreateMalloc - Generate the IR for a call to malloc:
415/// 1. Compute the malloc call's argument as the specified type's size,
416/// possibly multiplied by the array size if the array size is not
417/// constant 1.
418/// 2. Call malloc with that argument.
419/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000420Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
421 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000422 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000423 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000424 const Twine &Name) {
425 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000426 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427}
428
429/// CreateMalloc - Generate the IR for a call to malloc:
430/// 1. Compute the malloc call's argument as the specified type's size,
431/// possibly multiplied by the array size if the array size is not
432/// constant 1.
433/// 2. Call malloc with that argument.
434/// 3. Bitcast the result of the malloc call to the specified type.
435/// Note: This function does not add the bitcast to the basic block, that is the
436/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000437Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
438 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000439 Value *AllocSize, Value *ArraySize,
440 Function *MallocF, const Twine &Name) {
441 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000442 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000443}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000444
Victor Hernandeze2971492009-10-24 04:23:03 +0000445static Instruction* createFree(Value* Source, Instruction *InsertBefore,
446 BasicBlock *InsertAtEnd) {
447 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
448 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000449 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000450 "Can not free something of nonpointer type!");
451
452 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
453 Module* M = BB->getParent()->getParent();
454
455 const Type *VoidTy = Type::getVoidTy(M->getContext());
456 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
457 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000458 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000459 CallInst* Result = NULL;
460 Value *PtrCast = Source;
461 if (InsertBefore) {
462 if (Source->getType() != IntPtrTy)
463 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
464 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
465 } else {
466 if (Source->getType() != IntPtrTy)
467 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
468 Result = CallInst::Create(FreeFunc, PtrCast, "");
469 }
470 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000471 if (Function *F = dyn_cast<Function>(FreeFunc))
472 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000473
474 return Result;
475}
476
477/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000478Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
479 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000480}
481
482/// CreateFree - Generate the IR for a call to the builtin free function.
483/// Note: This function does not add the call to the basic block, that is the
484/// responsibility of the caller.
485Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
486 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
487 assert(FreeCall && "CreateFree did not create a CallInst");
488 return FreeCall;
489}
490
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000491//===----------------------------------------------------------------------===//
492// InvokeInst Implementation
493//===----------------------------------------------------------------------===//
494
495void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000496 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000497 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000498 Op<-3>() = Fn;
499 Op<-2>() = IfNormal;
500 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000501 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000502 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000503 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000504
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000505 assert(((NumArgs == FTy->getNumParams()) ||
506 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000507 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000508
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000509 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000510 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000511 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000512 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000513 "Invoking a function with a bad signature!");
514
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000515 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000516 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000517}
518
Misha Brukmanb1c93172005-04-21 23:48:37 +0000519InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000520 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000521 OperandTraits<InvokeInst>::op_end(this)
522 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000523 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000524 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000525 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000526 Use *OL = OperandList, *InOL = II.OperandList;
527 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000528 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000529 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000530}
531
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
533 return getSuccessor(idx);
534}
535unsigned InvokeInst::getNumSuccessorsV() const {
536 return getNumSuccessors();
537}
538void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
539 return setSuccessor(idx, B);
540}
541
Devang Patelba3fa6c2008-09-23 23:03:40 +0000542bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000543 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000544 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000545 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000546 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000547 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000548}
549
Devang Patel4c758ea2008-09-25 21:00:45 +0000550void InvokeInst::addAttribute(unsigned i, Attributes attr) {
551 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000552 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000553 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000554}
555
Devang Patel4c758ea2008-09-25 21:00:45 +0000556void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
557 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000558 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000559 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000560}
561
Duncan Sands5208d1a2007-11-28 17:07:01 +0000562
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000563//===----------------------------------------------------------------------===//
564// ReturnInst Implementation
565//===----------------------------------------------------------------------===//
566
Chris Lattner2195fc42007-02-24 00:55:48 +0000567ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000568 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000569 OperandTraits<ReturnInst>::op_end(this) -
570 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000571 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000572 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000573 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000574 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000575}
576
Owen Anderson55f1c092009-08-13 21:58:54 +0000577ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
578 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000579 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
580 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000581 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000582 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000583}
Owen Anderson55f1c092009-08-13 21:58:54 +0000584ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
585 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000586 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
587 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000588 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000589 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000590}
Owen Anderson55f1c092009-08-13 21:58:54 +0000591ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
592 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000593 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000594}
595
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596unsigned ReturnInst::getNumSuccessorsV() const {
597 return getNumSuccessors();
598}
599
Devang Patelae682fb2008-02-26 17:56:20 +0000600/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
601/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000603 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000604}
605
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000607 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608 return 0;
609}
610
Devang Patel59643e52008-02-23 00:35:18 +0000611ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000612}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000614//===----------------------------------------------------------------------===//
615// UnwindInst Implementation
616//===----------------------------------------------------------------------===//
617
Owen Anderson55f1c092009-08-13 21:58:54 +0000618UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
619 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
620 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000621}
Owen Anderson55f1c092009-08-13 21:58:54 +0000622UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
623 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
624 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000625}
626
627
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628unsigned UnwindInst::getNumSuccessorsV() const {
629 return getNumSuccessors();
630}
631
632void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000633 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000634}
635
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000637 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638 return 0;
639}
640
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000641//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000642// UnreachableInst Implementation
643//===----------------------------------------------------------------------===//
644
Owen Anderson55f1c092009-08-13 21:58:54 +0000645UnreachableInst::UnreachableInst(LLVMContext &Context,
646 Instruction *InsertBefore)
647 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
648 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000649}
Owen Anderson55f1c092009-08-13 21:58:54 +0000650UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
651 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
652 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000653}
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655unsigned UnreachableInst::getNumSuccessorsV() const {
656 return getNumSuccessors();
657}
658
659void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000660 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661}
662
663BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000664 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000666}
667
668//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000669// BranchInst Implementation
670//===----------------------------------------------------------------------===//
671
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672void BranchInst::AssertOK() {
673 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000674 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000675 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000676}
677
Chris Lattner2195fc42007-02-24 00:55:48 +0000678BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000679 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000680 OperandTraits<BranchInst>::op_end(this) - 1,
681 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000682 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000683 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000684}
685BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
686 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000687 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000688 OperandTraits<BranchInst>::op_end(this) - 3,
689 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000690 Op<-1>() = IfTrue;
691 Op<-2>() = IfFalse;
692 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000693#ifndef NDEBUG
694 AssertOK();
695#endif
696}
697
698BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000699 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000700 OperandTraits<BranchInst>::op_end(this) - 1,
701 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000702 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000703 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
705
706BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
707 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000708 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000709 OperandTraits<BranchInst>::op_end(this) - 3,
710 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000711 Op<-1>() = IfTrue;
712 Op<-2>() = IfFalse;
713 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000714#ifndef NDEBUG
715 AssertOK();
716#endif
717}
718
719
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000721 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000722 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
723 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000724 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 if (BI.getNumOperands() != 1) {
726 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000727 Op<-3>() = BI.Op<-3>();
728 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000730 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731}
732
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000733
734Use* Use::getPrefix() {
735 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
736 if (PotentialPrefix.getOpaqueValue())
737 return 0;
738
739 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
740}
741
742BranchInst::~BranchInst() {
743 if (NumOperands == 1) {
744 if (Use *Prefix = OperandList->getPrefix()) {
745 Op<-1>() = 0;
746 //
747 // mark OperandList to have a special value for scrutiny
748 // by baseclass destructors and operator delete
749 OperandList = Prefix;
750 } else {
751 NumOperands = 3;
752 OperandList = op_begin();
753 }
754 }
755}
756
757
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
759 return getSuccessor(idx);
760}
761unsigned BranchInst::getNumSuccessorsV() const {
762 return getNumSuccessors();
763}
764void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
765 setSuccessor(idx, B);
766}
767
768
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000769//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000770// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000771//===----------------------------------------------------------------------===//
772
Owen Andersonb6b25302009-07-14 23:09:55 +0000773static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000775 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000776 else {
777 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000778 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000779 assert(Amt->getType()->isIntegerTy() &&
780 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000781 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000782 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000783}
784
Victor Hernandez8acf2952009-10-23 21:09:37 +0000785AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
786 const Twine &Name, Instruction *InsertBefore)
787 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
788 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
789 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000790 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000791 setName(Name);
792}
793
794AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
795 const Twine &Name, BasicBlock *InsertAtEnd)
796 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
797 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
798 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000799 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000800 setName(Name);
801}
802
803AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
804 Instruction *InsertBefore)
805 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
806 getAISize(Ty->getContext(), 0), InsertBefore) {
807 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000808 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000809 setName(Name);
810}
811
812AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
813 BasicBlock *InsertAtEnd)
814 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
815 getAISize(Ty->getContext(), 0), InsertAtEnd) {
816 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000817 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000818 setName(Name);
819}
820
821AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
822 const Twine &Name, Instruction *InsertBefore)
823 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000824 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000825 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000826 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000827 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828}
829
Victor Hernandez8acf2952009-10-23 21:09:37 +0000830AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
831 const Twine &Name, BasicBlock *InsertAtEnd)
832 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000833 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000834 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000835 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000836 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000837}
838
Gordon Henriksen14a55692007-12-10 02:14:30 +0000839// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000840AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000841}
842
Victor Hernandez8acf2952009-10-23 21:09:37 +0000843void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000844 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000845 assert(Align <= MaximumAlignment &&
846 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000847 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000848 assert(getAlignment() == Align && "Alignment representation error!");
849}
850
Victor Hernandez8acf2952009-10-23 21:09:37 +0000851bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000852 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000853 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000854 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000855}
856
Victor Hernandez8acf2952009-10-23 21:09:37 +0000857const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858 return getType()->getElementType();
859}
860
Chris Lattner8b291e62008-11-26 02:54:17 +0000861/// isStaticAlloca - Return true if this alloca is in the entry block of the
862/// function and is a constant size. If so, the code generator will fold it
863/// into the prolog/epilog code, so it is basically free.
864bool AllocaInst::isStaticAlloca() const {
865 // Must be constant size.
866 if (!isa<ConstantInt>(getArraySize())) return false;
867
868 // Must be in the entry block.
869 const BasicBlock *Parent = getParent();
870 return Parent == &Parent->getParent()->front();
871}
872
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874// LoadInst Implementation
875//===----------------------------------------------------------------------===//
876
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000877void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000878 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880}
881
Daniel Dunbar4975db62009-07-25 04:41:11 +0000882LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000883 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000884 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000885 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000886 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000888 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889}
890
Daniel Dunbar4975db62009-07-25 04:41:11 +0000891LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000893 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000894 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000895 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000896 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000897 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898}
899
Daniel Dunbar4975db62009-07-25 04:41:11 +0000900LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000902 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000903 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000904 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000905 setAlignment(0);
906 AssertOK();
907 setName(Name);
908}
909
Daniel Dunbar4975db62009-07-25 04:41:11 +0000910LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000911 unsigned Align, Instruction *InsertBef)
912 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
913 Load, Ptr, InsertBef) {
914 setVolatile(isVolatile);
915 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000917 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000918}
919
Daniel Dunbar4975db62009-07-25 04:41:11 +0000920LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000921 unsigned Align, BasicBlock *InsertAE)
922 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
923 Load, Ptr, InsertAE) {
924 setVolatile(isVolatile);
925 setAlignment(Align);
926 AssertOK();
927 setName(Name);
928}
929
Daniel Dunbar4975db62009-07-25 04:41:11 +0000930LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000931 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000933 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000934 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000935 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000936 AssertOK();
937 setName(Name);
938}
939
Daniel Dunbar27096822009-08-11 18:11:15 +0000940
941
942LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
943 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
944 Load, Ptr, InsertBef) {
945 setVolatile(false);
946 setAlignment(0);
947 AssertOK();
948 if (Name && Name[0]) setName(Name);
949}
950
951LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
952 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
953 Load, Ptr, InsertAE) {
954 setVolatile(false);
955 setAlignment(0);
956 AssertOK();
957 if (Name && Name[0]) setName(Name);
958}
959
960LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
961 Instruction *InsertBef)
962: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
963 Load, Ptr, InsertBef) {
964 setVolatile(isVolatile);
965 setAlignment(0);
966 AssertOK();
967 if (Name && Name[0]) setName(Name);
968}
969
970LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
971 BasicBlock *InsertAE)
972 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
973 Load, Ptr, InsertAE) {
974 setVolatile(isVolatile);
975 setAlignment(0);
976 AssertOK();
977 if (Name && Name[0]) setName(Name);
978}
979
Christopher Lamb84485702007-04-22 19:24:39 +0000980void LoadInst::setAlignment(unsigned Align) {
981 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000982 assert(Align <= MaximumAlignment &&
983 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000984 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
985 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000986 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000987}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988
989//===----------------------------------------------------------------------===//
990// StoreInst Implementation
991//===----------------------------------------------------------------------===//
992
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000994 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000995 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000996 "Ptr must have pointer type!");
997 assert(getOperand(0)->getType() ==
998 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000999 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001000}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001001
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002
1003StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001004 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001005 OperandTraits<StoreInst>::op_begin(this),
1006 OperandTraits<StoreInst>::operands(this),
1007 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001008 Op<0>() = val;
1009 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001010 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001011 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001012 AssertOK();
1013}
1014
1015StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001016 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001017 OperandTraits<StoreInst>::op_begin(this),
1018 OperandTraits<StoreInst>::operands(this),
1019 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001020 Op<0>() = val;
1021 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001022 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001023 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001024 AssertOK();
1025}
1026
Misha Brukmanb1c93172005-04-21 23:48:37 +00001027StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001028 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001029 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001030 OperandTraits<StoreInst>::op_begin(this),
1031 OperandTraits<StoreInst>::operands(this),
1032 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001033 Op<0>() = val;
1034 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001035 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001036 setAlignment(0);
1037 AssertOK();
1038}
1039
1040StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1041 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001042 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001043 OperandTraits<StoreInst>::op_begin(this),
1044 OperandTraits<StoreInst>::operands(this),
1045 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001046 Op<0>() = val;
1047 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001048 setVolatile(isVolatile);
1049 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001050 AssertOK();
1051}
1052
Misha Brukmanb1c93172005-04-21 23:48:37 +00001053StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001054 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001055 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001056 OperandTraits<StoreInst>::op_begin(this),
1057 OperandTraits<StoreInst>::operands(this),
1058 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001059 Op<0>() = val;
1060 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001061 setVolatile(isVolatile);
1062 setAlignment(Align);
1063 AssertOK();
1064}
1065
1066StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001067 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001068 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001069 OperandTraits<StoreInst>::op_begin(this),
1070 OperandTraits<StoreInst>::operands(this),
1071 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001072 Op<0>() = val;
1073 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001074 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001075 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001076 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001077}
1078
Christopher Lamb84485702007-04-22 19:24:39 +00001079void StoreInst::setAlignment(unsigned Align) {
1080 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001081 assert(Align <= MaximumAlignment &&
1082 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001083 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1084 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001085 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001086}
1087
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001088//===----------------------------------------------------------------------===//
1089// GetElementPtrInst Implementation
1090//===----------------------------------------------------------------------===//
1091
Christopher Lambedf07882007-12-17 01:12:55 +00001092static unsigned retrieveAddrSpace(const Value *Val) {
1093 return cast<PointerType>(Val->getType())->getAddressSpace();
1094}
1095
Gabor Greif21ba1842008-06-06 20:28:12 +00001096void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001097 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001098 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1099 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001100 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001101
Chris Lattner79807c3d2007-01-31 19:47:18 +00001102 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001103 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001104
1105 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001106}
1107
Daniel Dunbar4975db62009-07-25 04:41:11 +00001108void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001109 assert(NumOperands == 2 && "NumOperands not initialized?");
1110 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001111 OL[0] = Ptr;
1112 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001113
1114 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001115}
1116
Gabor Greiff6caff662008-05-10 08:32:32 +00001117GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001118 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001119 OperandTraits<GetElementPtrInst>::op_end(this)
1120 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001121 GEPI.getNumOperands()) {
1122 Use *OL = OperandList;
1123 Use *GEPIOL = GEPI.OperandList;
1124 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001125 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001126 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001127}
1128
Chris Lattner82981202005-05-03 05:43:30 +00001129GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001130 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001131 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001132 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001133 GetElementPtr,
1134 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1135 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001136 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001137}
1138
1139GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001140 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001141 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001142 checkType(getIndexedType(Ptr->getType(),Idx)),
1143 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001144 GetElementPtr,
1145 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1146 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001147 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001148}
1149
Chris Lattner6090a422009-03-09 04:46:40 +00001150/// getIndexedType - Returns the type of the element that would be accessed with
1151/// a gep instruction with the specified parameters.
1152///
1153/// The Idxs pointer should point to a continuous piece of memory containing the
1154/// indices, either as Value* or uint64_t.
1155///
1156/// A null type is returned if the indices are invalid for the specified
1157/// pointer type.
1158///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001159template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001160static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1161 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001162 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1163 if (!PTy) return 0; // Type isn't a pointer type!
1164 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001165
Chris Lattner6090a422009-03-09 04:46:40 +00001166 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001167 if (NumIdx == 0)
1168 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001169
1170 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001171 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1172 // that contain opaque types) under the assumption that it will be resolved to
1173 // a sane type later.
1174 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001175 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001176
Dan Gohman1ecaf452008-05-31 00:58:22 +00001177 unsigned CurIdx = 1;
1178 for (; CurIdx != NumIdx; ++CurIdx) {
1179 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001180 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001181 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001182 if (!CT->indexValid(Index)) return 0;
1183 Agg = CT->getTypeAtIndex(Index);
1184
1185 // If the new type forwards to another type, then it is in the middle
1186 // of being refined to another type (and hence, may have dropped all
1187 // references to what it was using before). So, use the new forwarded
1188 // type.
1189 if (const Type *Ty = Agg->getForwardedType())
1190 Agg = Ty;
1191 }
1192 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193}
1194
Matthijs Kooijman04468622008-07-29 08:46:11 +00001195const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1196 Value* const *Idxs,
1197 unsigned NumIdx) {
1198 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1199}
1200
1201const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1202 uint64_t const *Idxs,
1203 unsigned NumIdx) {
1204 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1205}
1206
Chris Lattner82981202005-05-03 05:43:30 +00001207const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1208 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1209 if (!PTy) return 0; // Type isn't a pointer type!
1210
1211 // Check the pointer index.
1212 if (!PTy->indexValid(Idx)) return 0;
1213
Chris Lattnerc2233332005-05-03 16:44:45 +00001214 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001215}
1216
Chris Lattner45f15572007-04-14 00:12:57 +00001217
1218/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1219/// zeros. If so, the result pointer and the first operand have the same
1220/// value, just potentially different types.
1221bool GetElementPtrInst::hasAllZeroIndices() const {
1222 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1223 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1224 if (!CI->isZero()) return false;
1225 } else {
1226 return false;
1227 }
1228 }
1229 return true;
1230}
1231
Chris Lattner27058292007-04-27 20:35:56 +00001232/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1233/// constant integers. If so, the result pointer and the first operand have
1234/// a constant offset between them.
1235bool GetElementPtrInst::hasAllConstantIndices() const {
1236 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1237 if (!isa<ConstantInt>(getOperand(i)))
1238 return false;
1239 }
1240 return true;
1241}
1242
Dan Gohman1b849082009-09-07 23:54:19 +00001243void GetElementPtrInst::setIsInBounds(bool B) {
1244 cast<GEPOperator>(this)->setIsInBounds(B);
1245}
Chris Lattner45f15572007-04-14 00:12:57 +00001246
Nick Lewycky28a5f252009-09-27 21:33:04 +00001247bool GetElementPtrInst::isInBounds() const {
1248 return cast<GEPOperator>(this)->isInBounds();
1249}
1250
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001251//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001252// ExtractElementInst Implementation
1253//===----------------------------------------------------------------------===//
1254
1255ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001256 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001257 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001258 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001259 ExtractElement,
1260 OperandTraits<ExtractElementInst>::op_begin(this),
1261 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001262 assert(isValidOperands(Val, Index) &&
1263 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001264 Op<0>() = Val;
1265 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001266 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001267}
1268
1269ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001270 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001271 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001272 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001273 ExtractElement,
1274 OperandTraits<ExtractElementInst>::op_begin(this),
1275 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001276 assert(isValidOperands(Val, Index) &&
1277 "Invalid extractelement instruction operands!");
1278
Gabor Greif2d3024d2008-05-26 21:33:52 +00001279 Op<0>() = Val;
1280 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001281 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001282}
1283
Chris Lattner65511ff2006-10-05 06:24:58 +00001284
Chris Lattner54865b32006-04-08 04:05:48 +00001285bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001286 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001287 return false;
1288 return true;
1289}
1290
1291
Robert Bocchino23004482006-01-10 19:05:34 +00001292//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001293// InsertElementInst Implementation
1294//===----------------------------------------------------------------------===//
1295
Chris Lattner54865b32006-04-08 04:05:48 +00001296InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001297 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001298 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001299 : Instruction(Vec->getType(), InsertElement,
1300 OperandTraits<InsertElementInst>::op_begin(this),
1301 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001302 assert(isValidOperands(Vec, Elt, Index) &&
1303 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001304 Op<0>() = Vec;
1305 Op<1>() = Elt;
1306 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001307 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001308}
1309
Chris Lattner54865b32006-04-08 04:05:48 +00001310InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001311 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001312 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001313 : Instruction(Vec->getType(), InsertElement,
1314 OperandTraits<InsertElementInst>::op_begin(this),
1315 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001316 assert(isValidOperands(Vec, Elt, Index) &&
1317 "Invalid insertelement instruction operands!");
1318
Gabor Greif2d3024d2008-05-26 21:33:52 +00001319 Op<0>() = Vec;
1320 Op<1>() = Elt;
1321 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001322 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001323}
1324
Chris Lattner54865b32006-04-08 04:05:48 +00001325bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1326 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001327 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001328 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001329
Reid Spencerd84d35b2007-02-15 02:26:10 +00001330 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001331 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001332
Duncan Sands9dff9be2010-02-15 16:12:20 +00001333 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001334 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001335 return true;
1336}
1337
1338
Robert Bocchinoca27f032006-01-17 20:07:22 +00001339//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001340// ShuffleVectorInst Implementation
1341//===----------------------------------------------------------------------===//
1342
1343ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001344 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001345 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001346: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001347 cast<VectorType>(Mask->getType())->getNumElements()),
1348 ShuffleVector,
1349 OperandTraits<ShuffleVectorInst>::op_begin(this),
1350 OperandTraits<ShuffleVectorInst>::operands(this),
1351 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001352 assert(isValidOperands(V1, V2, Mask) &&
1353 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001354 Op<0>() = V1;
1355 Op<1>() = V2;
1356 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001357 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001358}
1359
1360ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001361 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001362 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001363: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1364 cast<VectorType>(Mask->getType())->getNumElements()),
1365 ShuffleVector,
1366 OperandTraits<ShuffleVectorInst>::op_begin(this),
1367 OperandTraits<ShuffleVectorInst>::operands(this),
1368 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001369 assert(isValidOperands(V1, V2, Mask) &&
1370 "Invalid shuffle vector instruction operands!");
1371
Gabor Greif2d3024d2008-05-26 21:33:52 +00001372 Op<0>() = V1;
1373 Op<1>() = V2;
1374 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001375 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001376}
1377
Mon P Wang25f01062008-11-10 04:46:22 +00001378bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001379 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001380 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001381 return false;
1382
1383 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001384 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001385 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001386
1387 // Check to see if Mask is valid.
1388 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1389 const VectorType *VTy = cast<VectorType>(V1->getType());
1390 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1391 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1392 if (CI->uge(VTy->getNumElements()*2))
1393 return false;
1394 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1395 return false;
1396 }
1397 }
1398 }
1399 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1400 return false;
1401
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001402 return true;
1403}
1404
Chris Lattnerf724e342008-03-02 05:28:33 +00001405/// getMaskValue - Return the index from the shuffle mask for the specified
1406/// output result. This is either -1 if the element is undef or a number less
1407/// than 2*numelements.
1408int ShuffleVectorInst::getMaskValue(unsigned i) const {
1409 const Constant *Mask = cast<Constant>(getOperand(2));
1410 if (isa<UndefValue>(Mask)) return -1;
1411 if (isa<ConstantAggregateZero>(Mask)) return 0;
1412 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1413 assert(i < MaskCV->getNumOperands() && "Index out of range");
1414
1415 if (isa<UndefValue>(MaskCV->getOperand(i)))
1416 return -1;
1417 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1418}
1419
Dan Gohman12fce772008-05-15 19:50:34 +00001420//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001421// InsertValueInst Class
1422//===----------------------------------------------------------------------===//
1423
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001424void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001425 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001426 assert(NumOperands == 2 && "NumOperands not initialized?");
1427 Op<0>() = Agg;
1428 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001429
Dan Gohmandd41bba2010-06-21 19:47:52 +00001430 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001431 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001432}
1433
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001434void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001435 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001436 assert(NumOperands == 2 && "NumOperands not initialized?");
1437 Op<0>() = Agg;
1438 Op<1>() = Val;
1439
1440 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001441 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001442}
1443
1444InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001445 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001446 OperandTraits<InsertValueInst>::op_begin(this), 2),
1447 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001448 Op<0>() = IVI.getOperand(0);
1449 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001450 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001451}
1452
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001453InsertValueInst::InsertValueInst(Value *Agg,
1454 Value *Val,
1455 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001456 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001457 Instruction *InsertBefore)
1458 : Instruction(Agg->getType(), InsertValue,
1459 OperandTraits<InsertValueInst>::op_begin(this),
1460 2, InsertBefore) {
1461 init(Agg, Val, Idx, Name);
1462}
1463
1464InsertValueInst::InsertValueInst(Value *Agg,
1465 Value *Val,
1466 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001467 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001468 BasicBlock *InsertAtEnd)
1469 : Instruction(Agg->getType(), InsertValue,
1470 OperandTraits<InsertValueInst>::op_begin(this),
1471 2, InsertAtEnd) {
1472 init(Agg, Val, Idx, Name);
1473}
1474
Dan Gohman0752bff2008-05-23 00:36:11 +00001475//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001476// ExtractValueInst Class
1477//===----------------------------------------------------------------------===//
1478
Gabor Greifcbcc4952008-06-06 21:06:32 +00001479void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001480 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001481 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001482
Dan Gohmandd41bba2010-06-21 19:47:52 +00001483 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001484 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001485}
1486
Daniel Dunbar4975db62009-07-25 04:41:11 +00001487void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001488 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001489
1490 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001491 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001492}
1493
1494ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001495 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001496 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001497 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001498}
1499
Dan Gohman12fce772008-05-15 19:50:34 +00001500// getIndexedType - Returns the type of the element that would be extracted
1501// with an extractvalue instruction with the specified parameters.
1502//
1503// A null type is returned if the indices are invalid for the specified
1504// pointer type.
1505//
1506const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001507 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001508 unsigned NumIdx) {
1509 unsigned CurIdx = 0;
1510 for (; CurIdx != NumIdx; ++CurIdx) {
1511 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001512 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001513 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001514 if (!CT->indexValid(Index)) return 0;
1515 Agg = CT->getTypeAtIndex(Index);
1516
1517 // If the new type forwards to another type, then it is in the middle
1518 // of being refined to another type (and hence, may have dropped all
1519 // references to what it was using before). So, use the new forwarded
1520 // type.
1521 if (const Type *Ty = Agg->getForwardedType())
1522 Agg = Ty;
1523 }
1524 return CurIdx == NumIdx ? Agg : 0;
1525}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001526
Dan Gohman4a3125b2008-06-17 21:07:55 +00001527const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001528 unsigned Idx) {
1529 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001530}
1531
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001532//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001533// BinaryOperator Class
1534//===----------------------------------------------------------------------===//
1535
Chris Lattner2195fc42007-02-24 00:55:48 +00001536BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001537 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001538 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001539 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001540 OperandTraits<BinaryOperator>::op_begin(this),
1541 OperandTraits<BinaryOperator>::operands(this),
1542 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001543 Op<0>() = S1;
1544 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001545 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001546 setName(Name);
1547}
1548
1549BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001550 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001551 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001552 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001553 OperandTraits<BinaryOperator>::op_begin(this),
1554 OperandTraits<BinaryOperator>::operands(this),
1555 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001556 Op<0>() = S1;
1557 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001558 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001559 setName(Name);
1560}
1561
1562
1563void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001564 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001565 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001566 assert(LHS->getType() == RHS->getType() &&
1567 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001568#ifndef NDEBUG
1569 switch (iType) {
1570 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001571 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001572 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001573 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001574 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001575 "Tried to create an integer operation on a non-integer type!");
1576 break;
1577 case FAdd: case FSub:
1578 case FMul:
1579 assert(getType() == LHS->getType() &&
1580 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001581 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001582 "Tried to create a floating-point operation on a "
1583 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001584 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001585 case UDiv:
1586 case SDiv:
1587 assert(getType() == LHS->getType() &&
1588 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001589 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001590 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001591 "Incorrect operand type (not integer) for S/UDIV");
1592 break;
1593 case FDiv:
1594 assert(getType() == LHS->getType() &&
1595 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001596 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001597 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001598 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001599 case URem:
1600 case SRem:
1601 assert(getType() == LHS->getType() &&
1602 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001603 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001604 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001605 "Incorrect operand type (not integer) for S/UREM");
1606 break;
1607 case FRem:
1608 assert(getType() == LHS->getType() &&
1609 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001610 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001611 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001612 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001613 case Shl:
1614 case LShr:
1615 case AShr:
1616 assert(getType() == LHS->getType() &&
1617 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001618 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001619 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001620 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001621 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001622 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001623 case And: case Or:
1624 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001625 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001626 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001627 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001628 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001629 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001630 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001632 default:
1633 break;
1634 }
1635#endif
1636}
1637
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001638BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001639 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001640 Instruction *InsertBefore) {
1641 assert(S1->getType() == S2->getType() &&
1642 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001643 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001644}
1645
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001646BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001647 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001648 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001649 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001650 InsertAtEnd->getInstList().push_back(Res);
1651 return Res;
1652}
1653
Dan Gohman5476cfd2009-08-12 16:23:25 +00001654BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001655 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001656 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001657 return new BinaryOperator(Instruction::Sub,
1658 zero, Op,
1659 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001660}
1661
Dan Gohman5476cfd2009-08-12 16:23:25 +00001662BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001663 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001664 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001665 return new BinaryOperator(Instruction::Sub,
1666 zero, Op,
1667 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001668}
1669
Dan Gohman4ab44202009-12-18 02:58:50 +00001670BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1671 Instruction *InsertBefore) {
1672 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1673 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1674}
1675
1676BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1677 BasicBlock *InsertAtEnd) {
1678 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1679 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1680}
1681
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001682BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1683 Instruction *InsertBefore) {
1684 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1685 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1686}
1687
1688BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1689 BasicBlock *InsertAtEnd) {
1690 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1691 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1692}
1693
Dan Gohman5476cfd2009-08-12 16:23:25 +00001694BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001695 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001696 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001697 return new BinaryOperator(Instruction::FSub,
1698 zero, Op,
1699 Op->getType(), Name, InsertBefore);
1700}
1701
Dan Gohman5476cfd2009-08-12 16:23:25 +00001702BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001703 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001704 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001705 return new BinaryOperator(Instruction::FSub,
1706 zero, Op,
1707 Op->getType(), Name, InsertAtEnd);
1708}
1709
Dan Gohman5476cfd2009-08-12 16:23:25 +00001710BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001711 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001712 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001713 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001714 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001715 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001716 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001717 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001718 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001719 }
1720
1721 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001722 Op->getType(), Name, InsertBefore);
1723}
1724
Dan Gohman5476cfd2009-08-12 16:23:25 +00001725BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001726 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001727 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001728 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001729 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001730 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001731 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001732 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001733 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001734 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001735 }
1736
1737 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001738 Op->getType(), Name, InsertAtEnd);
1739}
1740
1741
1742// isConstantAllOnes - Helper function for several functions below
1743static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001744 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1745 return CI->isAllOnesValue();
1746 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1747 return CV->isAllOnesValue();
1748 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001749}
1750
Owen Andersonbb2501b2009-07-13 22:18:28 +00001751bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001752 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1753 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001754 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1755 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001756 return false;
1757}
1758
Owen Andersonbb2501b2009-07-13 22:18:28 +00001759bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001760 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1761 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001762 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001763 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001764 return false;
1765}
1766
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001767bool BinaryOperator::isNot(const Value *V) {
1768 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1769 return (Bop->getOpcode() == Instruction::Xor &&
1770 (isConstantAllOnes(Bop->getOperand(1)) ||
1771 isConstantAllOnes(Bop->getOperand(0))));
1772 return false;
1773}
1774
Chris Lattner2c7d1772005-04-24 07:28:37 +00001775Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001776 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001777}
1778
Chris Lattner2c7d1772005-04-24 07:28:37 +00001779const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1780 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001781}
1782
Dan Gohmana5b96452009-06-04 22:49:04 +00001783Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001784 return cast<BinaryOperator>(BinOp)->getOperand(1);
1785}
1786
1787const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1788 return getFNegArgument(const_cast<Value*>(BinOp));
1789}
1790
Chris Lattner2c7d1772005-04-24 07:28:37 +00001791Value *BinaryOperator::getNotArgument(Value *BinOp) {
1792 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1793 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1794 Value *Op0 = BO->getOperand(0);
1795 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001796 if (isConstantAllOnes(Op0)) return Op1;
1797
1798 assert(isConstantAllOnes(Op1));
1799 return Op0;
1800}
1801
Chris Lattner2c7d1772005-04-24 07:28:37 +00001802const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1803 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001804}
1805
1806
1807// swapOperands - Exchange the two operands to this instruction. This
1808// instruction is safe to use on any binary instruction and does not
1809// modify the semantics of the instruction. If the instruction is
1810// order dependent (SetLT f.e.) the opcode is changed.
1811//
1812bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001813 if (!isCommutative())
1814 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001815 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001816 return false;
1817}
1818
Dan Gohman1b849082009-09-07 23:54:19 +00001819void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1820 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1821}
1822
1823void BinaryOperator::setHasNoSignedWrap(bool b) {
1824 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1825}
1826
1827void BinaryOperator::setIsExact(bool b) {
1828 cast<SDivOperator>(this)->setIsExact(b);
1829}
1830
Nick Lewycky28a5f252009-09-27 21:33:04 +00001831bool BinaryOperator::hasNoUnsignedWrap() const {
1832 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1833}
1834
1835bool BinaryOperator::hasNoSignedWrap() const {
1836 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1837}
1838
1839bool BinaryOperator::isExact() const {
1840 return cast<SDivOperator>(this)->isExact();
1841}
1842
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001843//===----------------------------------------------------------------------===//
1844// CastInst Class
1845//===----------------------------------------------------------------------===//
1846
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001847// Just determine if this cast only deals with integral->integral conversion.
1848bool CastInst::isIntegerCast() const {
1849 switch (getOpcode()) {
1850 default: return false;
1851 case Instruction::ZExt:
1852 case Instruction::SExt:
1853 case Instruction::Trunc:
1854 return true;
1855 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001856 return getOperand(0)->getType()->isIntegerTy() &&
1857 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001859}
1860
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001861bool CastInst::isLosslessCast() const {
1862 // Only BitCast can be lossless, exit fast if we're not BitCast
1863 if (getOpcode() != Instruction::BitCast)
1864 return false;
1865
1866 // Identity cast is always lossless
1867 const Type* SrcTy = getOperand(0)->getType();
1868 const Type* DstTy = getType();
1869 if (SrcTy == DstTy)
1870 return true;
1871
Reid Spencer8d9336d2006-12-31 05:26:44 +00001872 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001873 if (SrcTy->isPointerTy())
1874 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875 return false; // Other types have no identity values
1876}
1877
1878/// This function determines if the CastInst does not require any bits to be
1879/// changed in order to effect the cast. Essentially, it identifies cases where
1880/// no code gen is necessary for the cast, hence the name no-op cast. For
1881/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001882/// # bitcast i32* %x to i8*
1883/// # bitcast <2 x i32> %x to <4 x i16>
1884/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001885/// @brief Determine if the described cast is a no-op.
1886bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1887 const Type *SrcTy,
1888 const Type *DestTy,
1889 const Type *IntPtrTy) {
1890 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001891 default:
1892 assert(!"Invalid CastOp");
1893 case Instruction::Trunc:
1894 case Instruction::ZExt:
1895 case Instruction::SExt:
1896 case Instruction::FPTrunc:
1897 case Instruction::FPExt:
1898 case Instruction::UIToFP:
1899 case Instruction::SIToFP:
1900 case Instruction::FPToUI:
1901 case Instruction::FPToSI:
1902 return false; // These always modify bits
1903 case Instruction::BitCast:
1904 return true; // BitCast never modifies bits.
1905 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001906 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001907 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001908 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001909 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001910 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001911 }
1912}
1913
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001914/// @brief Determine if a cast is a no-op.
1915bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1916 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1917}
1918
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001919/// This function determines if a pair of casts can be eliminated and what
1920/// opcode should be used in the elimination. This assumes that there are two
1921/// instructions like this:
1922/// * %F = firstOpcode SrcTy %x to MidTy
1923/// * %S = secondOpcode MidTy %F to DstTy
1924/// The function returns a resultOpcode so these two casts can be replaced with:
1925/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1926/// If no such cast is permited, the function returns 0.
1927unsigned CastInst::isEliminableCastPair(
1928 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1929 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1930{
1931 // Define the 144 possibilities for these two cast instructions. The values
1932 // in this matrix determine what to do in a given situation and select the
1933 // case in the switch below. The rows correspond to firstOp, the columns
1934 // correspond to secondOp. In looking at the table below, keep in mind
1935 // the following cast properties:
1936 //
1937 // Size Compare Source Destination
1938 // Operator Src ? Size Type Sign Type Sign
1939 // -------- ------------ ------------------- ---------------------
1940 // TRUNC > Integer Any Integral Any
1941 // ZEXT < Integral Unsigned Integer Any
1942 // SEXT < Integral Signed Integer Any
1943 // FPTOUI n/a FloatPt n/a Integral Unsigned
1944 // FPTOSI n/a FloatPt n/a Integral Signed
1945 // UITOFP n/a Integral Unsigned FloatPt n/a
1946 // SITOFP n/a Integral Signed FloatPt n/a
1947 // FPTRUNC > FloatPt n/a FloatPt n/a
1948 // FPEXT < FloatPt n/a FloatPt n/a
1949 // PTRTOINT n/a Pointer n/a Integral Unsigned
1950 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001951 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001952 //
1953 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001954 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1955 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001956 // of the produced value (we no longer know the top-part is all zeros).
1957 // Further this conversion is often much more expensive for typical hardware,
1958 // and causes issues when building libgcc. We disallow fptosi+sext for the
1959 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960 const unsigned numCastOps =
1961 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1962 static const uint8_t CastResults[numCastOps][numCastOps] = {
1963 // T F F U S F F P I B -+
1964 // R Z S P P I I T P 2 N T |
1965 // U E E 2 2 2 2 R E I T C +- secondOp
1966 // N X X U S F F N X N 2 V |
1967 // C T T I I P P C T T P T -+
1968 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1969 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1970 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001971 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1972 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1974 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1975 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1976 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1977 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1978 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1979 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1980 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00001981
1982 // If either of the casts are a bitcast from scalar to vector, disallow the
1983 // merging.
1984 if ((firstOp == Instruction::BitCast &&
1985 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
1986 (secondOp == Instruction::BitCast &&
1987 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
1988 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001989
1990 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1991 [secondOp-Instruction::CastOpsBegin];
1992 switch (ElimCase) {
1993 case 0:
1994 // categorically disallowed
1995 return 0;
1996 case 1:
1997 // allowed, use first cast's opcode
1998 return firstOp;
1999 case 2:
2000 // allowed, use second cast's opcode
2001 return secondOp;
2002 case 3:
2003 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002004 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002005 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002006 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007 return firstOp;
2008 return 0;
2009 case 4:
2010 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002011 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002012 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013 return firstOp;
2014 return 0;
2015 case 5:
2016 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002017 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002018 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002019 return secondOp;
2020 return 0;
2021 case 6:
2022 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002023 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002024 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002025 return secondOp;
2026 return 0;
2027 case 7: {
2028 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002029 if (!IntPtrTy)
2030 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002031 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2032 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033 if (MidSize >= PtrSize)
2034 return Instruction::BitCast;
2035 return 0;
2036 }
2037 case 8: {
2038 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2039 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2040 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002041 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2042 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002043 if (SrcSize == DstSize)
2044 return Instruction::BitCast;
2045 else if (SrcSize < DstSize)
2046 return firstOp;
2047 return secondOp;
2048 }
2049 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2050 return Instruction::ZExt;
2051 case 10:
2052 // fpext followed by ftrunc is allowed if the bit size returned to is
2053 // the same as the original, in which case its just a bitcast
2054 if (SrcTy == DstTy)
2055 return Instruction::BitCast;
2056 return 0; // If the types are not the same we can't eliminate it.
2057 case 11:
2058 // bitcast followed by ptrtoint is allowed as long as the bitcast
2059 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002060 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061 return secondOp;
2062 return 0;
2063 case 12:
2064 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002065 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002066 return firstOp;
2067 return 0;
2068 case 13: {
2069 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002070 if (!IntPtrTy)
2071 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002072 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2073 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2074 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002075 if (SrcSize <= PtrSize && SrcSize == DstSize)
2076 return Instruction::BitCast;
2077 return 0;
2078 }
2079 case 99:
2080 // cast combination can't happen (error in input). This is for all cases
2081 // where the MidTy is not the same for the two cast instructions.
2082 assert(!"Invalid Cast Combination");
2083 return 0;
2084 default:
2085 assert(!"Error in CastResults table!!!");
2086 return 0;
2087 }
2088 return 0;
2089}
2090
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002091CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002092 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002093 // Construct and return the appropriate CastInst subclass
2094 switch (op) {
2095 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2096 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2097 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2098 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2099 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2100 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2101 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2102 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2103 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2104 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2105 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2106 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2107 default:
2108 assert(!"Invalid opcode provided");
2109 }
2110 return 0;
2111}
2112
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002113CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002114 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 // Construct and return the appropriate CastInst subclass
2116 switch (op) {
2117 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2118 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2119 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2120 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2121 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2122 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2123 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2124 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2125 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2126 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2127 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2128 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2129 default:
2130 assert(!"Invalid opcode provided");
2131 }
2132 return 0;
2133}
2134
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002135CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002136 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002137 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002138 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002139 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2140 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002141}
2142
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002143CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002144 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002145 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002146 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002147 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2148 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002149}
2150
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002151CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002152 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002153 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002154 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002155 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2156 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002157}
2158
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002159CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002160 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002161 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002162 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002163 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2164 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002165}
2166
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002167CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002168 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002169 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002170 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002171 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2172 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002173}
2174
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002175CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002176 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002177 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002178 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002179 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2180 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002181}
2182
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002183CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002184 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002185 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002186 assert(S->getType()->isPointerTy() && "Invalid cast");
2187 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002188 "Invalid cast");
2189
Duncan Sands9dff9be2010-02-15 16:12:20 +00002190 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002191 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2192 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002193}
2194
2195/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002197 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002198 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002199 assert(S->getType()->isPointerTy() && "Invalid cast");
2200 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002201 "Invalid cast");
2202
Duncan Sands9dff9be2010-02-15 16:12:20 +00002203 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002204 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2205 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002206}
2207
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002208CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002209 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002210 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002211 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002212 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002213 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2214 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002215 Instruction::CastOps opcode =
2216 (SrcBits == DstBits ? Instruction::BitCast :
2217 (SrcBits > DstBits ? Instruction::Trunc :
2218 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002219 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002220}
2221
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002222CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002223 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002224 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002225 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002226 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002227 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2228 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002229 Instruction::CastOps opcode =
2230 (SrcBits == DstBits ? Instruction::BitCast :
2231 (SrcBits > DstBits ? Instruction::Trunc :
2232 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002233 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002234}
2235
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002236CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002237 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002238 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002239 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002240 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002241 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2242 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002243 Instruction::CastOps opcode =
2244 (SrcBits == DstBits ? Instruction::BitCast :
2245 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002246 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002247}
2248
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002249CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002250 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002251 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002252 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002253 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002254 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2255 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002256 Instruction::CastOps opcode =
2257 (SrcBits == DstBits ? Instruction::BitCast :
2258 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002259 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002260}
2261
Duncan Sands55e50902008-01-06 10:12:28 +00002262// Check whether it is valid to call getCastOpcode for these types.
2263// This routine must be kept in sync with getCastOpcode.
2264bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2265 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2266 return false;
2267
2268 if (SrcTy == DestTy)
2269 return true;
2270
2271 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002272 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2273 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002274
2275 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002276 if (DestTy->isIntegerTy()) { // Casting to integral
2277 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002278 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002279 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002280 return true;
2281 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002282 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002283 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002284 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002285 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002286 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002287 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2288 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002289 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002290 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002291 return true;
2292 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002293 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002294 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002295 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002296 return false;
2297 }
2298 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002299 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002300 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002301 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002302 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002303 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002304 return DestPTy->getBitWidth() == SrcBits;
2305 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002306 } else if (DestTy->isPointerTy()) { // Casting to pointer
2307 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002308 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002309 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002310 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002311 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002312 return false;
2313 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002314 } else if (DestTy->isX86_MMXTy()) {
2315 return SrcBits == 64;
Gabor Greif697e94c2008-05-15 10:04:30 +00002316 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002317 return false;
2318 }
2319}
2320
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002321// Provide a way to get a "cast" where the cast opcode is inferred from the
2322// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002323// logic in the castIsValid function below. This axiom should hold:
2324// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2325// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002327// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002328Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002329CastInst::getCastOpcode(
2330 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331 // Get the bit sizes, we'll need these
2332 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002333 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2334 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002335
Duncan Sands55e50902008-01-06 10:12:28 +00002336 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2337 "Only first class types are castable!");
2338
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002340 if (DestTy->isIntegerTy()) { // Casting to integral
2341 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342 if (DestBits < SrcBits)
2343 return Trunc; // int -> smaller int
2344 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002345 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346 return SExt; // signed -> SEXT
2347 else
2348 return ZExt; // unsigned -> ZEXT
2349 } else {
2350 return BitCast; // Same size, No-op cast
2351 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002352 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002353 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354 return FPToSI; // FP -> sint
2355 else
2356 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002357 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002359 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002360 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361 return BitCast; // Same size, no-op cast
2362 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002363 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364 "Casting from a value that is not first-class type");
2365 return PtrToInt; // ptr -> int
2366 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002367 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2368 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002369 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370 return SIToFP; // sint -> FP
2371 else
2372 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002373 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374 if (DestBits < SrcBits) {
2375 return FPTrunc; // FP -> smaller FP
2376 } else if (DestBits > SrcBits) {
2377 return FPExt; // FP -> larger FP
2378 } else {
2379 return BitCast; // same size, no-op cast
2380 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002381 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002383 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002384 PTy = NULL;
2385 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002387 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002389 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2390 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002392 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002393 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002394 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002395 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002396 return BitCast; // float/int -> vector
Dale Johannesendd224d22010-09-30 23:57:10 +00002397 } else if (SrcTy->isX86_MMXTy()) {
2398 assert(DestPTy->getBitWidth()==64 &&
2399 "Casting X86_MMX to vector of wrong width");
2400 return BitCast; // MMX to 64-bit vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002402 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002404 } else if (DestTy->isPointerTy()) {
2405 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002407 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 return IntToPtr; // int -> ptr
2409 } else {
2410 assert(!"Casting pointer to other than pointer or int");
2411 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002412 } else if (DestTy->isX86_MMXTy()) {
Bill Wendling3c704172010-10-03 00:46:06 +00002413 if (isa<VectorType>(SrcTy)) {
2414 assert(cast<VectorType>(SrcTy)->getBitWidth() == 64 &&
Dale Johannesendd224d22010-09-30 23:57:10 +00002415 "Casting vector of wrong width to X86_MMX");
2416 return BitCast; // 64-bit vector to MMX
2417 } else {
2418 assert(!"Illegal cast to X86_MMX");
2419 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 } else {
2421 assert(!"Casting to type that is not first-class");
2422 }
2423
2424 // If we fall through to here we probably hit an assertion cast above
2425 // and assertions are not turned on. Anything we return is an error, so
2426 // BitCast is as good a choice as any.
2427 return BitCast;
2428}
2429
2430//===----------------------------------------------------------------------===//
2431// CastInst SubClass Constructors
2432//===----------------------------------------------------------------------===//
2433
2434/// Check that the construction parameters for a CastInst are correct. This
2435/// could be broken out into the separate constructors but it is useful to have
2436/// it in one place and to eliminate the redundant code for getting the sizes
2437/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002438bool
2439CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440
2441 // Check for type sanity on the arguments
2442 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002443 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2444 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445 return false;
2446
2447 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002448 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2449 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450
2451 // Switch on the opcode provided
2452 switch (op) {
2453 default: return false; // This is an input error
2454 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002455 return SrcTy->isIntOrIntVectorTy() &&
2456 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002458 return SrcTy->isIntOrIntVectorTy() &&
2459 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002461 return SrcTy->isIntOrIntVectorTy() &&
2462 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002464 return SrcTy->isFPOrFPVectorTy() &&
2465 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002466 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002468 return SrcTy->isFPOrFPVectorTy() &&
2469 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002470 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002473 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2474 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002475 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2476 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002477 SVTy->getNumElements() == DVTy->getNumElements();
2478 }
2479 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002480 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002483 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2484 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002485 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2486 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002487 SVTy->getNumElements() == DVTy->getNumElements();
2488 }
2489 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002490 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002492 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002494 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495 case Instruction::BitCast:
2496 // BitCast implies a no-op cast of type only. No bits change.
2497 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002498 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 return false;
2500
Duncan Sands55e50902008-01-06 10:12:28 +00002501 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 // these cases, the cast is okay if the source and destination bit widths
2503 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002504 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505 }
2506}
2507
2508TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002509 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002511 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512}
2513
2514TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002515 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002517 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518}
2519
2520ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002521 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002523 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524}
2525
2526ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002527 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002529 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530}
2531SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002532 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002534 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535}
2536
Jeff Cohencc08c832006-12-02 02:22:01 +00002537SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002538 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002540 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541}
2542
2543FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002544 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002546 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547}
2548
2549FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002550 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002552 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553}
2554
2555FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002556 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002558 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559}
2560
2561FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002562 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002564 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565}
2566
2567UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002568 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002570 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571}
2572
2573UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002574 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002576 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577}
2578
2579SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002580 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002582 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583}
2584
2585SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002586 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002588 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589}
2590
2591FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002592 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002594 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595}
2596
2597FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002598 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002600 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601}
2602
2603FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002604 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002606 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607}
2608
2609FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002610 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002612 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613}
2614
2615PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002616 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002618 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619}
2620
2621PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002622 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002624 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625}
2626
2627IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002628 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002630 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631}
2632
2633IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002634 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002636 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637}
2638
2639BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002640 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002642 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643}
2644
2645BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002646 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002648 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002650
2651//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002652// CmpInst Classes
2653//===----------------------------------------------------------------------===//
2654
Chris Lattneraec33da2010-01-22 06:25:37 +00002655void CmpInst::Anchor() const {}
2656
Nate Begemand2195702008-05-12 19:01:56 +00002657CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002658 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002659 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002660 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002661 OperandTraits<CmpInst>::op_begin(this),
2662 OperandTraits<CmpInst>::operands(this),
2663 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002664 Op<0>() = LHS;
2665 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002666 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002667 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002668}
Gabor Greiff6caff662008-05-10 08:32:32 +00002669
Nate Begemand2195702008-05-12 19:01:56 +00002670CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002671 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002672 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002673 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002674 OperandTraits<CmpInst>::op_begin(this),
2675 OperandTraits<CmpInst>::operands(this),
2676 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002677 Op<0>() = LHS;
2678 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002679 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002680 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002681}
2682
2683CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002684CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002685 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002686 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002687 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002688 if (InsertBefore)
2689 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2690 S1, S2, Name);
2691 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002692 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002693 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002694 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002695
2696 if (InsertBefore)
2697 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2698 S1, S2, Name);
2699 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002700 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002701 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002702}
2703
2704CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002705CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002706 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002707 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002708 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2709 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002710 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002711 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2712 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002713}
2714
2715void CmpInst::swapOperands() {
2716 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2717 IC->swapOperands();
2718 else
2719 cast<FCmpInst>(this)->swapOperands();
2720}
2721
2722bool CmpInst::isCommutative() {
2723 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2724 return IC->isCommutative();
2725 return cast<FCmpInst>(this)->isCommutative();
2726}
2727
2728bool CmpInst::isEquality() {
2729 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2730 return IC->isEquality();
2731 return cast<FCmpInst>(this)->isEquality();
2732}
2733
2734
Dan Gohman4e724382008-05-31 02:47:54 +00002735CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002736 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002737 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002738 case ICMP_EQ: return ICMP_NE;
2739 case ICMP_NE: return ICMP_EQ;
2740 case ICMP_UGT: return ICMP_ULE;
2741 case ICMP_ULT: return ICMP_UGE;
2742 case ICMP_UGE: return ICMP_ULT;
2743 case ICMP_ULE: return ICMP_UGT;
2744 case ICMP_SGT: return ICMP_SLE;
2745 case ICMP_SLT: return ICMP_SGE;
2746 case ICMP_SGE: return ICMP_SLT;
2747 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002748
Dan Gohman4e724382008-05-31 02:47:54 +00002749 case FCMP_OEQ: return FCMP_UNE;
2750 case FCMP_ONE: return FCMP_UEQ;
2751 case FCMP_OGT: return FCMP_ULE;
2752 case FCMP_OLT: return FCMP_UGE;
2753 case FCMP_OGE: return FCMP_ULT;
2754 case FCMP_OLE: return FCMP_UGT;
2755 case FCMP_UEQ: return FCMP_ONE;
2756 case FCMP_UNE: return FCMP_OEQ;
2757 case FCMP_UGT: return FCMP_OLE;
2758 case FCMP_ULT: return FCMP_OGE;
2759 case FCMP_UGE: return FCMP_OLT;
2760 case FCMP_ULE: return FCMP_OGT;
2761 case FCMP_ORD: return FCMP_UNO;
2762 case FCMP_UNO: return FCMP_ORD;
2763 case FCMP_TRUE: return FCMP_FALSE;
2764 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002765 }
2766}
2767
Reid Spencer266e42b2006-12-23 06:05:41 +00002768ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2769 switch (pred) {
2770 default: assert(! "Unknown icmp predicate!");
2771 case ICMP_EQ: case ICMP_NE:
2772 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2773 return pred;
2774 case ICMP_UGT: return ICMP_SGT;
2775 case ICMP_ULT: return ICMP_SLT;
2776 case ICMP_UGE: return ICMP_SGE;
2777 case ICMP_ULE: return ICMP_SLE;
2778 }
2779}
2780
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002781ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2782 switch (pred) {
2783 default: assert(! "Unknown icmp predicate!");
2784 case ICMP_EQ: case ICMP_NE:
2785 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2786 return pred;
2787 case ICMP_SGT: return ICMP_UGT;
2788 case ICMP_SLT: return ICMP_ULT;
2789 case ICMP_SGE: return ICMP_UGE;
2790 case ICMP_SLE: return ICMP_ULE;
2791 }
2792}
2793
Reid Spencer0286bc12007-02-28 22:00:54 +00002794/// Initialize a set of values that all satisfy the condition with C.
2795///
2796ConstantRange
2797ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2798 APInt Lower(C);
2799 APInt Upper(C);
2800 uint32_t BitWidth = C.getBitWidth();
2801 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002802 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002803 case ICmpInst::ICMP_EQ: Upper++; break;
2804 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002805 case ICmpInst::ICMP_ULT:
2806 Lower = APInt::getMinValue(BitWidth);
2807 // Check for an empty-set condition.
2808 if (Lower == Upper)
2809 return ConstantRange(BitWidth, /*isFullSet=*/false);
2810 break;
2811 case ICmpInst::ICMP_SLT:
2812 Lower = APInt::getSignedMinValue(BitWidth);
2813 // Check for an empty-set condition.
2814 if (Lower == Upper)
2815 return ConstantRange(BitWidth, /*isFullSet=*/false);
2816 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002817 case ICmpInst::ICMP_UGT:
2818 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002819 // Check for an empty-set condition.
2820 if (Lower == Upper)
2821 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002822 break;
2823 case ICmpInst::ICMP_SGT:
2824 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002825 // Check for an empty-set condition.
2826 if (Lower == Upper)
2827 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002828 break;
2829 case ICmpInst::ICMP_ULE:
2830 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002831 // Check for a full-set condition.
2832 if (Lower == Upper)
2833 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002834 break;
2835 case ICmpInst::ICMP_SLE:
2836 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002837 // Check for a full-set condition.
2838 if (Lower == Upper)
2839 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002840 break;
2841 case ICmpInst::ICMP_UGE:
2842 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002843 // Check for a full-set condition.
2844 if (Lower == Upper)
2845 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002846 break;
2847 case ICmpInst::ICMP_SGE:
2848 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002849 // Check for a full-set condition.
2850 if (Lower == Upper)
2851 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002852 break;
2853 }
2854 return ConstantRange(Lower, Upper);
2855}
2856
Dan Gohman4e724382008-05-31 02:47:54 +00002857CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002858 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002859 default: assert(!"Unknown cmp predicate!");
2860 case ICMP_EQ: case ICMP_NE:
2861 return pred;
2862 case ICMP_SGT: return ICMP_SLT;
2863 case ICMP_SLT: return ICMP_SGT;
2864 case ICMP_SGE: return ICMP_SLE;
2865 case ICMP_SLE: return ICMP_SGE;
2866 case ICMP_UGT: return ICMP_ULT;
2867 case ICMP_ULT: return ICMP_UGT;
2868 case ICMP_UGE: return ICMP_ULE;
2869 case ICMP_ULE: return ICMP_UGE;
2870
Reid Spencerd9436b62006-11-20 01:22:35 +00002871 case FCMP_FALSE: case FCMP_TRUE:
2872 case FCMP_OEQ: case FCMP_ONE:
2873 case FCMP_UEQ: case FCMP_UNE:
2874 case FCMP_ORD: case FCMP_UNO:
2875 return pred;
2876 case FCMP_OGT: return FCMP_OLT;
2877 case FCMP_OLT: return FCMP_OGT;
2878 case FCMP_OGE: return FCMP_OLE;
2879 case FCMP_OLE: return FCMP_OGE;
2880 case FCMP_UGT: return FCMP_ULT;
2881 case FCMP_ULT: return FCMP_UGT;
2882 case FCMP_UGE: return FCMP_ULE;
2883 case FCMP_ULE: return FCMP_UGE;
2884 }
2885}
2886
Reid Spencer266e42b2006-12-23 06:05:41 +00002887bool CmpInst::isUnsigned(unsigned short predicate) {
2888 switch (predicate) {
2889 default: return false;
2890 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2891 case ICmpInst::ICMP_UGE: return true;
2892 }
2893}
2894
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002895bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002896 switch (predicate) {
2897 default: return false;
2898 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2899 case ICmpInst::ICMP_SGE: return true;
2900 }
2901}
2902
2903bool CmpInst::isOrdered(unsigned short predicate) {
2904 switch (predicate) {
2905 default: return false;
2906 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2907 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2908 case FCmpInst::FCMP_ORD: return true;
2909 }
2910}
2911
2912bool CmpInst::isUnordered(unsigned short predicate) {
2913 switch (predicate) {
2914 default: return false;
2915 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2916 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2917 case FCmpInst::FCMP_UNO: return true;
2918 }
2919}
2920
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002921bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2922 switch(predicate) {
2923 default: return false;
2924 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2925 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2926 }
2927}
2928
2929bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2930 switch(predicate) {
2931 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2932 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2933 default: return false;
2934 }
2935}
2936
2937
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002938//===----------------------------------------------------------------------===//
2939// SwitchInst Implementation
2940//===----------------------------------------------------------------------===//
2941
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002942void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002943 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002944 ReservedSpace = 2+NumCases*2;
2945 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002946 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002947
Gabor Greif2d3024d2008-05-26 21:33:52 +00002948 OperandList[0] = Value;
2949 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002950}
2951
Chris Lattner2195fc42007-02-24 00:55:48 +00002952/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2953/// switch on and a default destination. The number of additional cases can
2954/// be specified here to make memory allocation more efficient. This
2955/// constructor can also autoinsert before another instruction.
2956SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2957 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002958 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2959 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002960 init(Value, Default, NumCases);
2961}
2962
2963/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2964/// switch on and a default destination. The number of additional cases can
2965/// be specified here to make memory allocation more efficient. This
2966/// constructor also autoinserts at the end of the specified BasicBlock.
2967SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2968 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002969 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2970 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002971 init(Value, Default, NumCases);
2972}
2973
Misha Brukmanb1c93172005-04-21 23:48:37 +00002974SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002975 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002976 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002977 Use *OL = OperandList, *InOL = SI.OperandList;
2978 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002979 OL[i] = InOL[i];
2980 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002981 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002982 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002983}
2984
Gordon Henriksen14a55692007-12-10 02:14:30 +00002985SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002986 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002987}
2988
2989
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002990/// addCase - Add an entry to the switch instruction...
2991///
Chris Lattner47ac1872005-02-24 05:32:09 +00002992void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002993 unsigned OpNo = NumOperands;
2994 if (OpNo+2 > ReservedSpace)
2995 resizeOperands(0); // Get more space!
2996 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002997 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002998 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002999 OperandList[OpNo] = OnVal;
3000 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003001}
3002
3003/// removeCase - This method removes the specified successor from the switch
3004/// instruction. Note that this cannot be used to remove the default
3005/// destination (successor #0).
3006///
3007void SwitchInst::removeCase(unsigned idx) {
3008 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003009 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3010
3011 unsigned NumOps = getNumOperands();
3012 Use *OL = OperandList;
3013
3014 // Move everything after this operand down.
3015 //
3016 // FIXME: we could just swap with the end of the list, then erase. However,
3017 // client might not expect this to happen. The code as it is thrashes the
3018 // use/def lists, which is kinda lame.
3019 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3020 OL[i-2] = OL[i];
3021 OL[i-2+1] = OL[i+1];
3022 }
3023
3024 // Nuke the last value.
3025 OL[NumOps-2].set(0);
3026 OL[NumOps-2+1].set(0);
3027 NumOperands = NumOps-2;
3028}
3029
3030/// resizeOperands - resize operands - This adjusts the length of the operands
3031/// list according to the following behavior:
3032/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003033/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003034/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3035/// 3. If NumOps == NumOperands, trim the reserved space.
3036///
3037void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003038 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003039 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003040 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003041 } else if (NumOps*2 > NumOperands) {
3042 // No resize needed.
3043 if (ReservedSpace >= NumOps) return;
3044 } else if (NumOps == NumOperands) {
3045 if (ReservedSpace == NumOps) return;
3046 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003047 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003048 }
3049
3050 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003051 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003052 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003053 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003054 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003055 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003056 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003057 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003058}
3059
3060
3061BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3062 return getSuccessor(idx);
3063}
3064unsigned SwitchInst::getNumSuccessorsV() const {
3065 return getNumSuccessors();
3066}
3067void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3068 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003069}
Chris Lattnerf22be932004-10-15 23:52:53 +00003070
Chris Lattner3ed871f2009-10-27 19:13:16 +00003071//===----------------------------------------------------------------------===//
3072// SwitchInst Implementation
3073//===----------------------------------------------------------------------===//
3074
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003075void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003076 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003077 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003078 ReservedSpace = 1+NumDests;
3079 NumOperands = 1;
3080 OperandList = allocHungoffUses(ReservedSpace);
3081
3082 OperandList[0] = Address;
3083}
3084
3085
3086/// resizeOperands - resize operands - This adjusts the length of the operands
3087/// list according to the following behavior:
3088/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3089/// of operation. This grows the number of ops by 2 times.
3090/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3091/// 3. If NumOps == NumOperands, trim the reserved space.
3092///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003093void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003094 unsigned e = getNumOperands();
3095 if (NumOps == 0) {
3096 NumOps = e*2;
3097 } else if (NumOps*2 > NumOperands) {
3098 // No resize needed.
3099 if (ReservedSpace >= NumOps) return;
3100 } else if (NumOps == NumOperands) {
3101 if (ReservedSpace == NumOps) return;
3102 } else {
3103 return;
3104 }
3105
3106 ReservedSpace = NumOps;
3107 Use *NewOps = allocHungoffUses(NumOps);
3108 Use *OldOps = OperandList;
3109 for (unsigned i = 0; i != e; ++i)
3110 NewOps[i] = OldOps[i];
3111 OperandList = NewOps;
3112 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3113}
3114
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003115IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3116 Instruction *InsertBefore)
3117: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003118 0, 0, InsertBefore) {
3119 init(Address, NumCases);
3120}
3121
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003122IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3123 BasicBlock *InsertAtEnd)
3124: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003125 0, 0, InsertAtEnd) {
3126 init(Address, NumCases);
3127}
3128
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003129IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3130 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003131 allocHungoffUses(IBI.getNumOperands()),
3132 IBI.getNumOperands()) {
3133 Use *OL = OperandList, *InOL = IBI.OperandList;
3134 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3135 OL[i] = InOL[i];
3136 SubclassOptionalData = IBI.SubclassOptionalData;
3137}
3138
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003139IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003140 dropHungoffUses(OperandList);
3141}
3142
3143/// addDestination - Add a destination.
3144///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003145void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003146 unsigned OpNo = NumOperands;
3147 if (OpNo+1 > ReservedSpace)
3148 resizeOperands(0); // Get more space!
3149 // Initialize some new operands.
3150 assert(OpNo < ReservedSpace && "Growing didn't work!");
3151 NumOperands = OpNo+1;
3152 OperandList[OpNo] = DestBB;
3153}
3154
3155/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003156/// indirectbr instruction.
3157void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003158 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3159
3160 unsigned NumOps = getNumOperands();
3161 Use *OL = OperandList;
3162
3163 // Replace this value with the last one.
3164 OL[idx+1] = OL[NumOps-1];
3165
3166 // Nuke the last value.
3167 OL[NumOps-1].set(0);
3168 NumOperands = NumOps-1;
3169}
3170
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003171BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003172 return getSuccessor(idx);
3173}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003174unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003175 return getNumSuccessors();
3176}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003177void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003178 setSuccessor(idx, B);
3179}
3180
3181//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003182// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003183//===----------------------------------------------------------------------===//
3184
Chris Lattnerf22be932004-10-15 23:52:53 +00003185// Define these methods here so vtables don't get emitted into every translation
3186// unit that uses these classes.
3187
Devang Patel11cf3f42009-10-27 22:16:29 +00003188GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3189 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003190}
3191
Devang Patel11cf3f42009-10-27 22:16:29 +00003192BinaryOperator *BinaryOperator::clone_impl() const {
3193 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003194}
3195
Devang Patel11cf3f42009-10-27 22:16:29 +00003196FCmpInst* FCmpInst::clone_impl() const {
3197 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003198}
3199
Devang Patel11cf3f42009-10-27 22:16:29 +00003200ICmpInst* ICmpInst::clone_impl() const {
3201 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003202}
3203
Devang Patel11cf3f42009-10-27 22:16:29 +00003204ExtractValueInst *ExtractValueInst::clone_impl() const {
3205 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003206}
3207
Devang Patel11cf3f42009-10-27 22:16:29 +00003208InsertValueInst *InsertValueInst::clone_impl() const {
3209 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003210}
3211
Devang Patel11cf3f42009-10-27 22:16:29 +00003212AllocaInst *AllocaInst::clone_impl() const {
3213 return new AllocaInst(getAllocatedType(),
3214 (Value*)getOperand(0),
3215 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003216}
3217
Devang Patel11cf3f42009-10-27 22:16:29 +00003218LoadInst *LoadInst::clone_impl() const {
3219 return new LoadInst(getOperand(0),
3220 Twine(), isVolatile(),
3221 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003222}
3223
Devang Patel11cf3f42009-10-27 22:16:29 +00003224StoreInst *StoreInst::clone_impl() const {
3225 return new StoreInst(getOperand(0), getOperand(1),
3226 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003227}
3228
Devang Patel11cf3f42009-10-27 22:16:29 +00003229TruncInst *TruncInst::clone_impl() const {
3230 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003231}
3232
Devang Patel11cf3f42009-10-27 22:16:29 +00003233ZExtInst *ZExtInst::clone_impl() const {
3234 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003235}
3236
Devang Patel11cf3f42009-10-27 22:16:29 +00003237SExtInst *SExtInst::clone_impl() const {
3238 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003239}
3240
Devang Patel11cf3f42009-10-27 22:16:29 +00003241FPTruncInst *FPTruncInst::clone_impl() const {
3242 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003243}
3244
Devang Patel11cf3f42009-10-27 22:16:29 +00003245FPExtInst *FPExtInst::clone_impl() const {
3246 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003247}
3248
Devang Patel11cf3f42009-10-27 22:16:29 +00003249UIToFPInst *UIToFPInst::clone_impl() const {
3250 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003251}
3252
Devang Patel11cf3f42009-10-27 22:16:29 +00003253SIToFPInst *SIToFPInst::clone_impl() const {
3254 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003255}
3256
Devang Patel11cf3f42009-10-27 22:16:29 +00003257FPToUIInst *FPToUIInst::clone_impl() const {
3258 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003259}
3260
Devang Patel11cf3f42009-10-27 22:16:29 +00003261FPToSIInst *FPToSIInst::clone_impl() const {
3262 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003263}
3264
Devang Patel11cf3f42009-10-27 22:16:29 +00003265PtrToIntInst *PtrToIntInst::clone_impl() const {
3266 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003267}
3268
Devang Patel11cf3f42009-10-27 22:16:29 +00003269IntToPtrInst *IntToPtrInst::clone_impl() const {
3270 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003271}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003272
Devang Patel11cf3f42009-10-27 22:16:29 +00003273BitCastInst *BitCastInst::clone_impl() const {
3274 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003275}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276
Devang Patel11cf3f42009-10-27 22:16:29 +00003277CallInst *CallInst::clone_impl() const {
3278 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003279}
3280
Devang Patel11cf3f42009-10-27 22:16:29 +00003281SelectInst *SelectInst::clone_impl() const {
3282 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003283}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003284
Devang Patel11cf3f42009-10-27 22:16:29 +00003285VAArgInst *VAArgInst::clone_impl() const {
3286 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003287}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003288
Devang Patel11cf3f42009-10-27 22:16:29 +00003289ExtractElementInst *ExtractElementInst::clone_impl() const {
3290 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003291}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003292
Devang Patel11cf3f42009-10-27 22:16:29 +00003293InsertElementInst *InsertElementInst::clone_impl() const {
3294 return InsertElementInst::Create(getOperand(0),
3295 getOperand(1),
3296 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003297}
3298
Devang Patel11cf3f42009-10-27 22:16:29 +00003299ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3300 return new ShuffleVectorInst(getOperand(0),
3301 getOperand(1),
3302 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003303}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003304
Devang Patel11cf3f42009-10-27 22:16:29 +00003305PHINode *PHINode::clone_impl() const {
3306 return new PHINode(*this);
3307}
3308
3309ReturnInst *ReturnInst::clone_impl() const {
3310 return new(getNumOperands()) ReturnInst(*this);
3311}
3312
3313BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003314 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003315 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003316}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003317
Devang Patel11cf3f42009-10-27 22:16:29 +00003318SwitchInst *SwitchInst::clone_impl() const {
3319 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003320}
3321
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003322IndirectBrInst *IndirectBrInst::clone_impl() const {
3323 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003324}
3325
3326
Devang Patel11cf3f42009-10-27 22:16:29 +00003327InvokeInst *InvokeInst::clone_impl() const {
3328 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003329}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003330
Devang Patel11cf3f42009-10-27 22:16:29 +00003331UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003332 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003333 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003334}
3335
Devang Patel11cf3f42009-10-27 22:16:29 +00003336UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003337 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003338 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003339}