blob: 84adc50994a76400ba26c88d8dac919e9351a4e8 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattnerf7b6d312005-05-06 20:26:43 +000026unsigned CallSite::getCallingConv() const {
27 if (CallInst *CI = dyn_cast<CallInst>(I))
28 return CI->getCallingConv();
29 else
30 return cast<InvokeInst>(I)->getCallingConv();
31}
32void CallSite::setCallingConv(unsigned CC) {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 CI->setCallingConv(CC);
35 else
36 cast<InvokeInst>(I)->setCallingConv(CC);
37}
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000038const ParamAttrsList* CallSite::getParamAttrs() const {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 return CI->getParamAttrs();
41 else
42 return cast<InvokeInst>(I)->getParamAttrs();
43}
44void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
45 if (CallInst *CI = dyn_cast<CallInst>(I))
46 CI->setParamAttrs(PAL);
47 else
48 cast<InvokeInst>(I)->setParamAttrs(PAL);
49}
Duncan Sands5208d1a2007-11-28 17:07:01 +000050bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
51 if (CallInst *CI = dyn_cast<CallInst>(I))
52 return CI->paramHasAttr(i, attr);
53 else
54 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
55}
Duncan Sands38ef3a82007-12-03 20:06:50 +000056bool CallSite::doesNotAccessMemory() const {
57 if (CallInst *CI = dyn_cast<CallInst>(I))
58 return CI->doesNotAccessMemory();
59 else
60 return cast<InvokeInst>(I)->doesNotAccessMemory();
61}
62bool CallSite::onlyReadsMemory() const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->onlyReadsMemory();
65 else
66 return cast<InvokeInst>(I)->onlyReadsMemory();
67}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000068
Chris Lattner1c12a882006-06-21 16:53:47 +000069
Chris Lattnerafdb3de2005-01-29 00:35:16 +000070//===----------------------------------------------------------------------===//
71// PHINode Class
72//===----------------------------------------------------------------------===//
73
74PHINode::PHINode(const PHINode &PN)
75 : Instruction(PN.getType(), Instruction::PHI,
76 new Use[PN.getNumOperands()], PN.getNumOperands()),
77 ReservedSpace(PN.getNumOperands()) {
78 Use *OL = OperandList;
79 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
80 OL[i].init(PN.getOperand(i), this);
81 OL[i+1].init(PN.getOperand(i+1), this);
82 }
83}
84
Gordon Henriksen3e5be662007-12-09 22:46:10 +000085void PHINode::destroyThis(PHINode*v) {
86 delete [] v->OperandList;
87 Instruction::destroyThis(v);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000088}
89
90// removeIncomingValue - Remove an incoming value. This is useful if a
91// predecessor basic block is deleted.
92Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
93 unsigned NumOps = getNumOperands();
94 Use *OL = OperandList;
95 assert(Idx*2 < NumOps && "BB not in PHI node!");
96 Value *Removed = OL[Idx*2];
97
98 // Move everything after this operand down.
99 //
100 // FIXME: we could just swap with the end of the list, then erase. However,
101 // client might not expect this to happen. The code as it is thrashes the
102 // use/def lists, which is kinda lame.
103 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
104 OL[i-2] = OL[i];
105 OL[i-2+1] = OL[i+1];
106 }
107
108 // Nuke the last value.
109 OL[NumOps-2].set(0);
110 OL[NumOps-2+1].set(0);
111 NumOperands = NumOps-2;
112
113 // If the PHI node is dead, because it has zero entries, nuke it now.
114 if (NumOps == 2 && DeletePHIIfEmpty) {
115 // If anyone is using this PHI, make them use a dummy value instead...
116 replaceAllUsesWith(UndefValue::get(getType()));
117 eraseFromParent();
118 }
119 return Removed;
120}
121
122/// resizeOperands - resize operands - This adjusts the length of the operands
123/// list according to the following behavior:
124/// 1. If NumOps == 0, grow the operand list in response to a push_back style
125/// of operation. This grows the number of ops by 1.5 times.
126/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
127/// 3. If NumOps == NumOperands, trim the reserved space.
128///
129void PHINode::resizeOperands(unsigned NumOps) {
130 if (NumOps == 0) {
131 NumOps = (getNumOperands())*3/2;
132 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
133 } else if (NumOps*2 > NumOperands) {
134 // No resize needed.
135 if (ReservedSpace >= NumOps) return;
136 } else if (NumOps == NumOperands) {
137 if (ReservedSpace == NumOps) return;
138 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000139 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140 }
141
142 ReservedSpace = NumOps;
143 Use *NewOps = new Use[NumOps];
144 Use *OldOps = OperandList;
145 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
146 NewOps[i].init(OldOps[i], this);
147 OldOps[i].set(0);
148 }
149 delete [] OldOps;
150 OperandList = NewOps;
151}
152
Nate Begemanb3923212005-08-04 23:24:19 +0000153/// hasConstantValue - If the specified PHI node always merges together the same
154/// value, return the value, otherwise return null.
155///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000156Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000157 // If the PHI node only has one incoming value, eliminate the PHI node...
158 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000159 if (getIncomingValue(0) != this) // not X = phi X
160 return getIncomingValue(0);
161 else
162 return UndefValue::get(getType()); // Self cycle is dead.
163
Nate Begemanb3923212005-08-04 23:24:19 +0000164 // Otherwise if all of the incoming values are the same for the PHI, replace
165 // the PHI node with the incoming value.
166 //
167 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000168 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000169 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000170 if (isa<UndefValue>(getIncomingValue(i)))
171 HasUndefInput = true;
172 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000173 if (InVal && getIncomingValue(i) != InVal)
174 return 0; // Not the same, bail out.
175 else
176 InVal = getIncomingValue(i);
177
178 // The only case that could cause InVal to be null is if we have a PHI node
179 // that only has entries for itself. In this case, there is no entry into the
180 // loop, so kill the PHI.
181 //
182 if (InVal == 0) InVal = UndefValue::get(getType());
183
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000184 // If we have a PHI node like phi(X, undef, X), where X is defined by some
185 // instruction, we cannot always return X as the result of the PHI node. Only
186 // do this if X is not an instruction (thus it must dominate the PHI block),
187 // or if the client is prepared to deal with this possibility.
188 if (HasUndefInput && !AllowNonDominatingInstruction)
189 if (Instruction *IV = dyn_cast<Instruction>(InVal))
190 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000191 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000192 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000193 return 0; // Cannot guarantee that InVal dominates this PHINode.
194
Nate Begemanb3923212005-08-04 23:24:19 +0000195 // All of the incoming values are the same, return the value now.
196 return InVal;
197}
198
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000199
200//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000201// CallInst Implementation
202//===----------------------------------------------------------------------===//
203
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000204void CallInst::destroyThis(CallInst*v) {
205 delete [] v->OperandList;
206 if (v->ParamAttrs)
207 v->ParamAttrs->dropRef();
208 Instruction::destroyThis(v);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000209}
210
Chris Lattner054ba2c2007-02-13 00:58:44 +0000211void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000212 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000213 NumOperands = NumParams+1;
214 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000216
Misha Brukmanb1c93172005-04-21 23:48:37 +0000217 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000218 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000219 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000220
Chris Lattner054ba2c2007-02-13 00:58:44 +0000221 assert((NumParams == FTy->getNumParams() ||
222 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000223 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000224 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000225 assert((i >= FTy->getNumParams() ||
226 FTy->getParamType(i) == Params[i]->getType()) &&
227 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000229 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000230}
231
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000233 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000234 NumOperands = 3;
235 Use *OL = OperandList = new Use[3];
236 OL[0].init(Func, this);
237 OL[1].init(Actual1, this);
238 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000239
240 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000241 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000242 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000243
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000244 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000245 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000247 assert((0 >= FTy->getNumParams() ||
248 FTy->getParamType(0) == Actual1->getType()) &&
249 "Calling a function with a bad signature!");
250 assert((1 >= FTy->getNumParams() ||
251 FTy->getParamType(1) == Actual2->getType()) &&
252 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253}
254
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000255void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000256 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257 NumOperands = 2;
258 Use *OL = OperandList = new Use[2];
259 OL[0].init(Func, this);
260 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000261
262 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000264 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000266 assert((FTy->getNumParams() == 1 ||
267 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000269 assert((0 == FTy->getNumParams() ||
270 FTy->getParamType(0) == Actual->getType()) &&
271 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272}
273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000275 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000276 NumOperands = 1;
277 Use *OL = OperandList = new Use[1];
278 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000279
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000280 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000282 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000284 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285}
286
David Greene17a5dfe2007-08-01 03:43:44 +0000287#if 0
288// Leave for llvm-gcc
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000289CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000290 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
David Greene17a5dfe2007-08-01 03:43:44 +0000292 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000293 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000294 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000295 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000296}
297CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
298 const std::string &Name, Instruction *InsertBefore)
David Greene17a5dfe2007-08-01 03:43:44 +0000299 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
300 ->getElementType())->getReturnType(),
301 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000302 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000303 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000304}
305
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
307 const std::string &Name, Instruction *InsertBefore)
308 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
309 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000310 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000312 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
315CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
316 const std::string &Name, BasicBlock *InsertAtEnd)
317 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
318 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000319 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000321 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322}
David Greene17a5dfe2007-08-01 03:43:44 +0000323#endif
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000325 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
327 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000330 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
333CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
334 BasicBlock *InsertAtEnd)
335 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
336 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000337 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000338 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000339 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341CallInst::CallInst(Value *Func, const std::string &Name,
342 Instruction *InsertBefore)
343 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
344 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000345 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000347 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348}
349
350CallInst::CallInst(Value *Func, const std::string &Name,
351 BasicBlock *InsertAtEnd)
352 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
353 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000354 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000356 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357}
358
Misha Brukmanb1c93172005-04-21 23:48:37 +0000359CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000360 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000361 CI.getNumOperands()),
362 ParamAttrs(0) {
363 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000364 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000365 Use *OL = OperandList;
366 Use *InOL = CI.OperandList;
367 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
368 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369}
370
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000371void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
372 if (ParamAttrs == newAttrs)
373 return;
374
Reid Spencerc6a83842007-04-22 17:28:03 +0000375 if (ParamAttrs)
376 ParamAttrs->dropRef();
377
378 if (newAttrs)
379 newAttrs->addRef();
380
381 ParamAttrs = newAttrs;
382}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383
Duncan Sands5208d1a2007-11-28 17:07:01 +0000384bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
385 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
386 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000387 if (const Function *F = getCalledFunction())
388 return F->paramHasAttr(i, attr);
389 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000390}
391
Duncan Sands5208d1a2007-11-28 17:07:01 +0000392
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000393//===----------------------------------------------------------------------===//
394// InvokeInst Implementation
395//===----------------------------------------------------------------------===//
396
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000397void InvokeInst::destroyThis(InvokeInst*v) {
398 delete [] v->OperandList;
399 if (v->ParamAttrs)
400 v->ParamAttrs->dropRef();
401 TerminatorInst::destroyThis(v);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402}
403
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000404void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000405 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000406 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000407 NumOperands = 3+NumArgs;
408 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000409 OL[0].init(Fn, this);
410 OL[1].init(IfNormal, this);
411 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000412 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000413 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000414 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000415
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000416 assert((NumArgs == FTy->getNumParams()) ||
417 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000418 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000419
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000420 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000421 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000422 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000423 "Invoking a function with a bad signature!");
424
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000425 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000426 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000427}
428
Misha Brukmanb1c93172005-04-21 23:48:37 +0000429InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000430 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000431 new Use[II.getNumOperands()], II.getNumOperands()),
432 ParamAttrs(0) {
433 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000434 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000435 Use *OL = OperandList, *InOL = II.OperandList;
436 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
437 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000438}
439
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000440BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
441 return getSuccessor(idx);
442}
443unsigned InvokeInst::getNumSuccessorsV() const {
444 return getNumSuccessors();
445}
446void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
447 return setSuccessor(idx, B);
448}
449
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000450void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
451 if (ParamAttrs == newAttrs)
452 return;
453
Reid Spencerc6a83842007-04-22 17:28:03 +0000454 if (ParamAttrs)
455 ParamAttrs->dropRef();
456
457 if (newAttrs)
458 newAttrs->addRef();
459
460 ParamAttrs = newAttrs;
461}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000462
Duncan Sands5208d1a2007-11-28 17:07:01 +0000463bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
464 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
465 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000466 if (const Function *F = getCalledFunction())
467 return F->paramHasAttr(i, attr);
468 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000469}
470
Duncan Sands5208d1a2007-11-28 17:07:01 +0000471
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000472//===----------------------------------------------------------------------===//
473// ReturnInst Implementation
474//===----------------------------------------------------------------------===//
475
Chris Lattner2195fc42007-02-24 00:55:48 +0000476ReturnInst::ReturnInst(const ReturnInst &RI)
477 : TerminatorInst(Type::VoidTy, Instruction::Ret,
478 &RetVal, RI.getNumOperands()) {
479 if (RI.getNumOperands())
480 RetVal.init(RI.RetVal, this);
481}
482
483ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
484 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
485 init(retVal);
486}
487ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
488 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
489 init(retVal);
490}
491ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
492 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
493}
494
495
496
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000497void ReturnInst::init(Value *retVal) {
498 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000499 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000500 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000501 NumOperands = 1;
502 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000503 }
504}
505
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000506unsigned ReturnInst::getNumSuccessorsV() const {
507 return getNumSuccessors();
508}
509
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000510// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
511// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000512void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000513 assert(0 && "ReturnInst has no successors!");
514}
515
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000516BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
517 assert(0 && "ReturnInst has no successors!");
518 abort();
519 return 0;
520}
521
522
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000523//===----------------------------------------------------------------------===//
524// UnwindInst Implementation
525//===----------------------------------------------------------------------===//
526
Chris Lattner2195fc42007-02-24 00:55:48 +0000527UnwindInst::UnwindInst(Instruction *InsertBefore)
528 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
529}
530UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
531 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
532}
533
534
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000535unsigned UnwindInst::getNumSuccessorsV() const {
536 return getNumSuccessors();
537}
538
539void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540 assert(0 && "UnwindInst has no successors!");
541}
542
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
544 assert(0 && "UnwindInst has no successors!");
545 abort();
546 return 0;
547}
548
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000549//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000550// UnreachableInst Implementation
551//===----------------------------------------------------------------------===//
552
Chris Lattner2195fc42007-02-24 00:55:48 +0000553UnreachableInst::UnreachableInst(Instruction *InsertBefore)
554 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
555}
556UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
557 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
558}
559
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000560unsigned UnreachableInst::getNumSuccessorsV() const {
561 return getNumSuccessors();
562}
563
564void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
565 assert(0 && "UnwindInst has no successors!");
566}
567
568BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
569 assert(0 && "UnwindInst has no successors!");
570 abort();
571 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000572}
573
574//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575// BranchInst Implementation
576//===----------------------------------------------------------------------===//
577
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578void BranchInst::AssertOK() {
579 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000580 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000581 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000582}
583
Chris Lattner2195fc42007-02-24 00:55:48 +0000584BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
585 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
586 assert(IfTrue != 0 && "Branch destination may not be null!");
587 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
588}
589BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
590 Instruction *InsertBefore)
591: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
592 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
593 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
594 Ops[2].init(Cond, this);
595#ifndef NDEBUG
596 AssertOK();
597#endif
598}
599
600BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
601 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
602 assert(IfTrue != 0 && "Branch destination may not be null!");
603 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
604}
605
606BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
607 BasicBlock *InsertAtEnd)
608 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
609 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
610 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
611 Ops[2].init(Cond, this);
612#ifndef NDEBUG
613 AssertOK();
614#endif
615}
616
617
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000618BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000619 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000620 OperandList[0].init(BI.getOperand(0), this);
621 if (BI.getNumOperands() != 1) {
622 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
623 OperandList[1].init(BI.getOperand(1), this);
624 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000625 }
626}
627
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
629 return getSuccessor(idx);
630}
631unsigned BranchInst::getNumSuccessorsV() const {
632 return getNumSuccessors();
633}
634void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
635 setSuccessor(idx, B);
636}
637
638
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639//===----------------------------------------------------------------------===//
640// AllocationInst Implementation
641//===----------------------------------------------------------------------===//
642
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000643static Value *getAISize(Value *Amt) {
644 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000645 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000646 else {
647 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000648 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000649 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000650 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000651 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000652 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000653}
654
Misha Brukmanb1c93172005-04-21 23:48:37 +0000655AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000656 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000657 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000659 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000660 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000662 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663}
664
Misha Brukmanb1c93172005-04-21 23:48:37 +0000665AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000666 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000667 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000668 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000669 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000670 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000672 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000673}
674
675bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000676 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
677 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000679}
680
681const Type *AllocationInst::getAllocatedType() const {
682 return getType()->getElementType();
683}
684
685AllocaInst::AllocaInst(const AllocaInst &AI)
686 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000687 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688}
689
690MallocInst::MallocInst(const MallocInst &MI)
691 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000692 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000693}
694
695//===----------------------------------------------------------------------===//
696// FreeInst Implementation
697//===----------------------------------------------------------------------===//
698
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000699void FreeInst::AssertOK() {
700 assert(isa<PointerType>(getOperand(0)->getType()) &&
701 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702}
703
704FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000705 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000707}
708
709FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000710 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000712}
713
714
715//===----------------------------------------------------------------------===//
716// LoadInst Implementation
717//===----------------------------------------------------------------------===//
718
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000720 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722}
723
724LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000726 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000727 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000728 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000729 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000730 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731}
732
733LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000735 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000736 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000737 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000738 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000739 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000740}
741
742LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
743 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000745 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000746 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000747 setAlignment(0);
748 AssertOK();
749 setName(Name);
750}
751
752LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
753 unsigned Align, Instruction *InsertBef)
754 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
755 Load, Ptr, InsertBef) {
756 setVolatile(isVolatile);
757 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000759 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760}
761
Dan Gohman68659282007-07-18 20:51:11 +0000762LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
763 unsigned Align, BasicBlock *InsertAE)
764 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
765 Load, Ptr, InsertAE) {
766 setVolatile(isVolatile);
767 setAlignment(Align);
768 AssertOK();
769 setName(Name);
770}
771
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000772LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
773 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000775 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000776 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000777 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000778 AssertOK();
779 setName(Name);
780}
781
782
783
784LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000785 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
786 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000787 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000788 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000789 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000790 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000791}
792
793LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000794 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
795 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000796 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000797 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000798 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000799 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000800}
801
802LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
803 Instruction *InsertBef)
804: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000805 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000806 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000807 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000808 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000809 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000810}
811
812LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
813 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000814 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
815 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000816 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000817 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000819 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820}
821
Christopher Lamb84485702007-04-22 19:24:39 +0000822void LoadInst::setAlignment(unsigned Align) {
823 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
824 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
825}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000826
827//===----------------------------------------------------------------------===//
828// StoreInst Implementation
829//===----------------------------------------------------------------------===//
830
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000831void StoreInst::AssertOK() {
832 assert(isa<PointerType>(getOperand(1)->getType()) &&
833 "Ptr must have pointer type!");
834 assert(getOperand(0)->getType() ==
835 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000836 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000837}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000838
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000839
840StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000841 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000842 Ops[0].init(val, this);
843 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000844 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000845 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000846 AssertOK();
847}
848
849StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000850 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000851 Ops[0].init(val, this);
852 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000853 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000854 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000855 AssertOK();
856}
857
Misha Brukmanb1c93172005-04-21 23:48:37 +0000858StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000859 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000860 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000861 Ops[0].init(val, this);
862 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000863 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000864 setAlignment(0);
865 AssertOK();
866}
867
868StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
869 unsigned Align, Instruction *InsertBefore)
870 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
871 Ops[0].init(val, this);
872 Ops[1].init(addr, this);
873 setVolatile(isVolatile);
874 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000875 AssertOK();
876}
877
Misha Brukmanb1c93172005-04-21 23:48:37 +0000878StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000879 unsigned Align, BasicBlock *InsertAtEnd)
880 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
881 Ops[0].init(val, this);
882 Ops[1].init(addr, this);
883 setVolatile(isVolatile);
884 setAlignment(Align);
885 AssertOK();
886}
887
888StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000889 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000890 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000891 Ops[0].init(val, this);
892 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000893 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000894 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000895 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000896}
897
Christopher Lamb84485702007-04-22 19:24:39 +0000898void StoreInst::setAlignment(unsigned Align) {
899 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
900 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
901}
902
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903//===----------------------------------------------------------------------===//
904// GetElementPtrInst Implementation
905//===----------------------------------------------------------------------===//
906
Chris Lattner79807c3d2007-01-31 19:47:18 +0000907void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
908 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000909 Use *OL = OperandList = new Use[NumOperands];
910 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000911
Chris Lattner79807c3d2007-01-31 19:47:18 +0000912 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000914}
915
Chris Lattner82981202005-05-03 05:43:30 +0000916void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
917 NumOperands = 2;
918 Use *OL = OperandList = new Use[2];
919 OL[0].init(Ptr, this);
920 OL[1].init(Idx, this);
921}
922
Chris Lattner82981202005-05-03 05:43:30 +0000923GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
924 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000925 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
926 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000927 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000928 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000929}
930
931GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
932 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000933 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
934 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000935 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000936 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000937}
938
Gordon Henriksen3e5be662007-12-09 22:46:10 +0000939void GetElementPtrInst::destroyThis(GetElementPtrInst*v) {
940 delete[] v->OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941}
942
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000943// getIndexedType - Returns the type of the element that would be loaded with
944// a load instruction with the specified parameters.
945//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000946// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000947// pointer type.
948//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000949const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000950 Value* const *Idxs,
951 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000952 bool AllowCompositeLeaf) {
953 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
954
955 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000956 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000957 if (AllowCompositeLeaf ||
958 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
959 return cast<PointerType>(Ptr)->getElementType();
960 else
961 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000962
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000963 unsigned CurIdx = 0;
964 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000965 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000966 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
967 return 0; // Can't load a whole structure or array!?!?
968 }
969
Chris Lattner302116a2007-01-31 04:40:28 +0000970 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000971 if (isa<PointerType>(CT) && CurIdx != 1)
972 return 0; // Can only index into pointer types at the first index!
973 if (!CT->indexValid(Index)) return 0;
974 Ptr = CT->getTypeAtIndex(Index);
975
976 // If the new type forwards to another type, then it is in the middle
977 // of being refined to another type (and hence, may have dropped all
978 // references to what it was using before). So, use the new forwarded
979 // type.
980 if (const Type * Ty = Ptr->getForwardedType()) {
981 Ptr = Ty;
982 }
983 }
Chris Lattner302116a2007-01-31 04:40:28 +0000984 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000985}
986
Chris Lattner82981202005-05-03 05:43:30 +0000987const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
988 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
989 if (!PTy) return 0; // Type isn't a pointer type!
990
991 // Check the pointer index.
992 if (!PTy->indexValid(Idx)) return 0;
993
Chris Lattnerc2233332005-05-03 16:44:45 +0000994 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000995}
996
Chris Lattner45f15572007-04-14 00:12:57 +0000997
998/// hasAllZeroIndices - Return true if all of the indices of this GEP are
999/// zeros. If so, the result pointer and the first operand have the same
1000/// value, just potentially different types.
1001bool GetElementPtrInst::hasAllZeroIndices() const {
1002 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1003 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1004 if (!CI->isZero()) return false;
1005 } else {
1006 return false;
1007 }
1008 }
1009 return true;
1010}
1011
Chris Lattner27058292007-04-27 20:35:56 +00001012/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1013/// constant integers. If so, the result pointer and the first operand have
1014/// a constant offset between them.
1015bool GetElementPtrInst::hasAllConstantIndices() const {
1016 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1017 if (!isa<ConstantInt>(getOperand(i)))
1018 return false;
1019 }
1020 return true;
1021}
1022
Chris Lattner45f15572007-04-14 00:12:57 +00001023
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001024//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001025// ExtractElementInst Implementation
1026//===----------------------------------------------------------------------===//
1027
1028ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001029 const std::string &Name,
1030 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001031 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001032 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001033 assert(isValidOperands(Val, Index) &&
1034 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001035 Ops[0].init(Val, this);
1036 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001037 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001038}
1039
Chris Lattner65511ff2006-10-05 06:24:58 +00001040ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1041 const std::string &Name,
1042 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001043 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001044 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001045 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001046 assert(isValidOperands(Val, Index) &&
1047 "Invalid extractelement instruction operands!");
1048 Ops[0].init(Val, this);
1049 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001050 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001051}
1052
1053
Robert Bocchino23004482006-01-10 19:05:34 +00001054ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001055 const std::string &Name,
1056 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001057 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001058 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001059 assert(isValidOperands(Val, Index) &&
1060 "Invalid extractelement instruction operands!");
1061
Robert Bocchino23004482006-01-10 19:05:34 +00001062 Ops[0].init(Val, this);
1063 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001064 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001065}
1066
Chris Lattner65511ff2006-10-05 06:24:58 +00001067ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1068 const std::string &Name,
1069 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001070 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001071 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001072 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001073 assert(isValidOperands(Val, Index) &&
1074 "Invalid extractelement instruction operands!");
1075
1076 Ops[0].init(Val, this);
1077 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001078 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001079}
1080
1081
Chris Lattner54865b32006-04-08 04:05:48 +00001082bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001083 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001084 return false;
1085 return true;
1086}
1087
1088
Robert Bocchino23004482006-01-10 19:05:34 +00001089//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001090// InsertElementInst Implementation
1091//===----------------------------------------------------------------------===//
1092
Chris Lattner0875d942006-04-14 22:20:32 +00001093InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1094 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1095 Ops[0].init(IE.Ops[0], this);
1096 Ops[1].init(IE.Ops[1], this);
1097 Ops[2].init(IE.Ops[2], this);
1098}
Chris Lattner54865b32006-04-08 04:05:48 +00001099InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001100 const std::string &Name,
1101 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001102 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001103 assert(isValidOperands(Vec, Elt, Index) &&
1104 "Invalid insertelement instruction operands!");
1105 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001106 Ops[1].init(Elt, this);
1107 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001108 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001109}
1110
Chris Lattner65511ff2006-10-05 06:24:58 +00001111InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1112 const std::string &Name,
1113 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001114 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001115 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001116 assert(isValidOperands(Vec, Elt, Index) &&
1117 "Invalid insertelement instruction operands!");
1118 Ops[0].init(Vec, this);
1119 Ops[1].init(Elt, this);
1120 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001121 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001122}
1123
1124
Chris Lattner54865b32006-04-08 04:05:48 +00001125InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001126 const std::string &Name,
1127 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001128 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001129 assert(isValidOperands(Vec, Elt, Index) &&
1130 "Invalid insertelement instruction operands!");
1131
1132 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001133 Ops[1].init(Elt, this);
1134 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001135 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001136}
1137
Chris Lattner65511ff2006-10-05 06:24:58 +00001138InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1139 const std::string &Name,
1140 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001141: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001142 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001143 assert(isValidOperands(Vec, Elt, Index) &&
1144 "Invalid insertelement instruction operands!");
1145
1146 Ops[0].init(Vec, this);
1147 Ops[1].init(Elt, this);
1148 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001149 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001150}
1151
Chris Lattner54865b32006-04-08 04:05:48 +00001152bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1153 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001154 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001155 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001156
Reid Spencerd84d35b2007-02-15 02:26:10 +00001157 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001158 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001159
Reid Spencer8d9336d2006-12-31 05:26:44 +00001160 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001161 return false; // Third operand of insertelement must be uint.
1162 return true;
1163}
1164
1165
Robert Bocchinoca27f032006-01-17 20:07:22 +00001166//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001167// ShuffleVectorInst Implementation
1168//===----------------------------------------------------------------------===//
1169
Chris Lattner0875d942006-04-14 22:20:32 +00001170ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1171 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1172 Ops[0].init(SV.Ops[0], this);
1173 Ops[1].init(SV.Ops[1], this);
1174 Ops[2].init(SV.Ops[2], this);
1175}
1176
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001177ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1178 const std::string &Name,
1179 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001180 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001181 assert(isValidOperands(V1, V2, Mask) &&
1182 "Invalid shuffle vector instruction operands!");
1183 Ops[0].init(V1, this);
1184 Ops[1].init(V2, this);
1185 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001186 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001187}
1188
1189ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1190 const std::string &Name,
1191 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001192 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001193 assert(isValidOperands(V1, V2, Mask) &&
1194 "Invalid shuffle vector instruction operands!");
1195
1196 Ops[0].init(V1, this);
1197 Ops[1].init(V2, this);
1198 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001199 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001200}
1201
1202bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1203 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001204 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001205 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001206 if (!isa<VectorType>(Mask->getType()) ||
1207 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1208 cast<VectorType>(Mask->getType())->getNumElements() !=
1209 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001210 return false;
1211 return true;
1212}
1213
1214
1215//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001216// BinaryOperator Class
1217//===----------------------------------------------------------------------===//
1218
Chris Lattner2195fc42007-02-24 00:55:48 +00001219BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1220 const Type *Ty, const std::string &Name,
1221 Instruction *InsertBefore)
1222 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1223 Ops[0].init(S1, this);
1224 Ops[1].init(S2, this);
1225 init(iType);
1226 setName(Name);
1227}
1228
1229BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1230 const Type *Ty, const std::string &Name,
1231 BasicBlock *InsertAtEnd)
1232 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1233 Ops[0].init(S1, this);
1234 Ops[1].init(S2, this);
1235 init(iType);
1236 setName(Name);
1237}
1238
1239
1240void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001241 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001242 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001243 assert(LHS->getType() == RHS->getType() &&
1244 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001245#ifndef NDEBUG
1246 switch (iType) {
1247 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001248 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001249 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001250 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001251 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001252 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001253 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001254 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001255 case UDiv:
1256 case SDiv:
1257 assert(getType() == LHS->getType() &&
1258 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001259 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1260 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001261 "Incorrect operand type (not integer) for S/UDIV");
1262 break;
1263 case FDiv:
1264 assert(getType() == LHS->getType() &&
1265 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001266 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1267 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001268 && "Incorrect operand type (not floating point) for FDIV");
1269 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001270 case URem:
1271 case SRem:
1272 assert(getType() == LHS->getType() &&
1273 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001274 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1275 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001276 "Incorrect operand type (not integer) for S/UREM");
1277 break;
1278 case FRem:
1279 assert(getType() == LHS->getType() &&
1280 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001281 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1282 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001283 && "Incorrect operand type (not floating point) for FREM");
1284 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001285 case Shl:
1286 case LShr:
1287 case AShr:
1288 assert(getType() == LHS->getType() &&
1289 "Shift operation should return same type as operands!");
1290 assert(getType()->isInteger() &&
1291 "Shift operation requires integer operands");
1292 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001293 case And: case Or:
1294 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001295 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001296 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001297 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001298 (isa<VectorType>(getType()) &&
1299 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001300 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001301 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001302 default:
1303 break;
1304 }
1305#endif
1306}
1307
1308BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001309 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001310 Instruction *InsertBefore) {
1311 assert(S1->getType() == S2->getType() &&
1312 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001313 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001314}
1315
1316BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001317 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001318 BasicBlock *InsertAtEnd) {
1319 BinaryOperator *Res = create(Op, S1, S2, Name);
1320 InsertAtEnd->getInstList().push_back(Res);
1321 return Res;
1322}
1323
1324BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1325 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001326 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1327 return new BinaryOperator(Instruction::Sub,
1328 zero, Op,
1329 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001330}
1331
1332BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1333 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001334 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1335 return new BinaryOperator(Instruction::Sub,
1336 zero, Op,
1337 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001338}
1339
1340BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1341 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001342 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001343 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001344 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001345 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001346 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001347 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001348 }
1349
1350 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001351 Op->getType(), Name, InsertBefore);
1352}
1353
1354BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1355 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001356 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001357 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001358 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001359 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001360 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001361 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001362 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001363 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001364 }
1365
1366 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001367 Op->getType(), Name, InsertAtEnd);
1368}
1369
1370
1371// isConstantAllOnes - Helper function for several functions below
1372static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001373 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1374 return CI->isAllOnesValue();
1375 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1376 return CV->isAllOnesValue();
1377 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001378}
1379
1380bool BinaryOperator::isNeg(const Value *V) {
1381 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1382 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001383 return Bop->getOperand(0) ==
1384 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001385 return false;
1386}
1387
1388bool BinaryOperator::isNot(const Value *V) {
1389 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1390 return (Bop->getOpcode() == Instruction::Xor &&
1391 (isConstantAllOnes(Bop->getOperand(1)) ||
1392 isConstantAllOnes(Bop->getOperand(0))));
1393 return false;
1394}
1395
Chris Lattner2c7d1772005-04-24 07:28:37 +00001396Value *BinaryOperator::getNegArgument(Value *BinOp) {
1397 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1398 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001399}
1400
Chris Lattner2c7d1772005-04-24 07:28:37 +00001401const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1402 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001403}
1404
Chris Lattner2c7d1772005-04-24 07:28:37 +00001405Value *BinaryOperator::getNotArgument(Value *BinOp) {
1406 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1407 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1408 Value *Op0 = BO->getOperand(0);
1409 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001410 if (isConstantAllOnes(Op0)) return Op1;
1411
1412 assert(isConstantAllOnes(Op1));
1413 return Op0;
1414}
1415
Chris Lattner2c7d1772005-04-24 07:28:37 +00001416const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1417 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001418}
1419
1420
1421// swapOperands - Exchange the two operands to this instruction. This
1422// instruction is safe to use on any binary instruction and does not
1423// modify the semantics of the instruction. If the instruction is
1424// order dependent (SetLT f.e.) the opcode is changed.
1425//
1426bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001427 if (!isCommutative())
1428 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001429 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001430 return false;
1431}
1432
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001433//===----------------------------------------------------------------------===//
1434// CastInst Class
1435//===----------------------------------------------------------------------===//
1436
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001437// Just determine if this cast only deals with integral->integral conversion.
1438bool CastInst::isIntegerCast() const {
1439 switch (getOpcode()) {
1440 default: return false;
1441 case Instruction::ZExt:
1442 case Instruction::SExt:
1443 case Instruction::Trunc:
1444 return true;
1445 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001446 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001447 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001448}
1449
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001450bool CastInst::isLosslessCast() const {
1451 // Only BitCast can be lossless, exit fast if we're not BitCast
1452 if (getOpcode() != Instruction::BitCast)
1453 return false;
1454
1455 // Identity cast is always lossless
1456 const Type* SrcTy = getOperand(0)->getType();
1457 const Type* DstTy = getType();
1458 if (SrcTy == DstTy)
1459 return true;
1460
Reid Spencer8d9336d2006-12-31 05:26:44 +00001461 // Pointer to pointer is always lossless.
1462 if (isa<PointerType>(SrcTy))
1463 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001464 return false; // Other types have no identity values
1465}
1466
1467/// This function determines if the CastInst does not require any bits to be
1468/// changed in order to effect the cast. Essentially, it identifies cases where
1469/// no code gen is necessary for the cast, hence the name no-op cast. For
1470/// example, the following are all no-op casts:
1471/// # bitcast uint %X, int
1472/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001473/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001474/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1475/// @brief Determine if a cast is a no-op.
1476bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1477 switch (getOpcode()) {
1478 default:
1479 assert(!"Invalid CastOp");
1480 case Instruction::Trunc:
1481 case Instruction::ZExt:
1482 case Instruction::SExt:
1483 case Instruction::FPTrunc:
1484 case Instruction::FPExt:
1485 case Instruction::UIToFP:
1486 case Instruction::SIToFP:
1487 case Instruction::FPToUI:
1488 case Instruction::FPToSI:
1489 return false; // These always modify bits
1490 case Instruction::BitCast:
1491 return true; // BitCast never modifies bits.
1492 case Instruction::PtrToInt:
1493 return IntPtrTy->getPrimitiveSizeInBits() ==
1494 getType()->getPrimitiveSizeInBits();
1495 case Instruction::IntToPtr:
1496 return IntPtrTy->getPrimitiveSizeInBits() ==
1497 getOperand(0)->getType()->getPrimitiveSizeInBits();
1498 }
1499}
1500
1501/// This function determines if a pair of casts can be eliminated and what
1502/// opcode should be used in the elimination. This assumes that there are two
1503/// instructions like this:
1504/// * %F = firstOpcode SrcTy %x to MidTy
1505/// * %S = secondOpcode MidTy %F to DstTy
1506/// The function returns a resultOpcode so these two casts can be replaced with:
1507/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1508/// If no such cast is permited, the function returns 0.
1509unsigned CastInst::isEliminableCastPair(
1510 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1511 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1512{
1513 // Define the 144 possibilities for these two cast instructions. The values
1514 // in this matrix determine what to do in a given situation and select the
1515 // case in the switch below. The rows correspond to firstOp, the columns
1516 // correspond to secondOp. In looking at the table below, keep in mind
1517 // the following cast properties:
1518 //
1519 // Size Compare Source Destination
1520 // Operator Src ? Size Type Sign Type Sign
1521 // -------- ------------ ------------------- ---------------------
1522 // TRUNC > Integer Any Integral Any
1523 // ZEXT < Integral Unsigned Integer Any
1524 // SEXT < Integral Signed Integer Any
1525 // FPTOUI n/a FloatPt n/a Integral Unsigned
1526 // FPTOSI n/a FloatPt n/a Integral Signed
1527 // UITOFP n/a Integral Unsigned FloatPt n/a
1528 // SITOFP n/a Integral Signed FloatPt n/a
1529 // FPTRUNC > FloatPt n/a FloatPt n/a
1530 // FPEXT < FloatPt n/a FloatPt n/a
1531 // PTRTOINT n/a Pointer n/a Integral Unsigned
1532 // INTTOPTR n/a Integral Unsigned Pointer n/a
1533 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001534 //
1535 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1536 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1537 // into "fptoui double to ulong", but this loses information about the range
1538 // of the produced value (we no longer know the top-part is all zeros).
1539 // Further this conversion is often much more expensive for typical hardware,
1540 // and causes issues when building libgcc. We disallow fptosi+sext for the
1541 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001542 const unsigned numCastOps =
1543 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1544 static const uint8_t CastResults[numCastOps][numCastOps] = {
1545 // T F F U S F F P I B -+
1546 // R Z S P P I I T P 2 N T |
1547 // U E E 2 2 2 2 R E I T C +- secondOp
1548 // N X X U S F F N X N 2 V |
1549 // C T T I I P P C T T P T -+
1550 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1551 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1552 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001553 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1554 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001555 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1556 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1557 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1558 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1559 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1560 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1561 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1562 };
1563
1564 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1565 [secondOp-Instruction::CastOpsBegin];
1566 switch (ElimCase) {
1567 case 0:
1568 // categorically disallowed
1569 return 0;
1570 case 1:
1571 // allowed, use first cast's opcode
1572 return firstOp;
1573 case 2:
1574 // allowed, use second cast's opcode
1575 return secondOp;
1576 case 3:
1577 // no-op cast in second op implies firstOp as long as the DestTy
1578 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001579 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001580 return firstOp;
1581 return 0;
1582 case 4:
1583 // no-op cast in second op implies firstOp as long as the DestTy
1584 // is floating point
1585 if (DstTy->isFloatingPoint())
1586 return firstOp;
1587 return 0;
1588 case 5:
1589 // no-op cast in first op implies secondOp as long as the SrcTy
1590 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001591 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001592 return secondOp;
1593 return 0;
1594 case 6:
1595 // no-op cast in first op implies secondOp as long as the SrcTy
1596 // is a floating point
1597 if (SrcTy->isFloatingPoint())
1598 return secondOp;
1599 return 0;
1600 case 7: {
1601 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1602 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1603 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1604 if (MidSize >= PtrSize)
1605 return Instruction::BitCast;
1606 return 0;
1607 }
1608 case 8: {
1609 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1610 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1611 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1612 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1613 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1614 if (SrcSize == DstSize)
1615 return Instruction::BitCast;
1616 else if (SrcSize < DstSize)
1617 return firstOp;
1618 return secondOp;
1619 }
1620 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1621 return Instruction::ZExt;
1622 case 10:
1623 // fpext followed by ftrunc is allowed if the bit size returned to is
1624 // the same as the original, in which case its just a bitcast
1625 if (SrcTy == DstTy)
1626 return Instruction::BitCast;
1627 return 0; // If the types are not the same we can't eliminate it.
1628 case 11:
1629 // bitcast followed by ptrtoint is allowed as long as the bitcast
1630 // is a pointer to pointer cast.
1631 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1632 return secondOp;
1633 return 0;
1634 case 12:
1635 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1636 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1637 return firstOp;
1638 return 0;
1639 case 13: {
1640 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1641 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1642 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1643 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1644 if (SrcSize <= PtrSize && SrcSize == DstSize)
1645 return Instruction::BitCast;
1646 return 0;
1647 }
1648 case 99:
1649 // cast combination can't happen (error in input). This is for all cases
1650 // where the MidTy is not the same for the two cast instructions.
1651 assert(!"Invalid Cast Combination");
1652 return 0;
1653 default:
1654 assert(!"Error in CastResults table!!!");
1655 return 0;
1656 }
1657 return 0;
1658}
1659
1660CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1661 const std::string &Name, Instruction *InsertBefore) {
1662 // Construct and return the appropriate CastInst subclass
1663 switch (op) {
1664 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1665 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1666 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1667 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1668 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1669 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1670 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1671 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1672 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1673 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1674 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1675 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1676 default:
1677 assert(!"Invalid opcode provided");
1678 }
1679 return 0;
1680}
1681
1682CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1683 const std::string &Name, BasicBlock *InsertAtEnd) {
1684 // Construct and return the appropriate CastInst subclass
1685 switch (op) {
1686 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1687 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1688 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1689 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1690 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1691 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1692 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1693 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1694 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1695 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1696 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1697 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1698 default:
1699 assert(!"Invalid opcode provided");
1700 }
1701 return 0;
1702}
1703
Reid Spencer5c140882006-12-04 20:17:56 +00001704CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1705 const std::string &Name,
1706 Instruction *InsertBefore) {
1707 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1708 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1709 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1710}
1711
1712CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1713 const std::string &Name,
1714 BasicBlock *InsertAtEnd) {
1715 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1716 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1717 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1718}
1719
1720CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1721 const std::string &Name,
1722 Instruction *InsertBefore) {
1723 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1724 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1725 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1726}
1727
1728CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1729 const std::string &Name,
1730 BasicBlock *InsertAtEnd) {
1731 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1732 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1733 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1734}
1735
1736CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1737 const std::string &Name,
1738 Instruction *InsertBefore) {
1739 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1740 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1741 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1742}
1743
1744CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1745 const std::string &Name,
1746 BasicBlock *InsertAtEnd) {
1747 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1748 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1749 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1750}
1751
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001752CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1753 const std::string &Name,
1754 BasicBlock *InsertAtEnd) {
1755 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001756 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001757 "Invalid cast");
1758
Chris Lattner03c49532007-01-15 02:27:26 +00001759 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001760 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1761 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1762}
1763
1764/// @brief Create a BitCast or a PtrToInt cast instruction
1765CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1766 const std::string &Name,
1767 Instruction *InsertBefore) {
1768 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001769 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001770 "Invalid cast");
1771
Chris Lattner03c49532007-01-15 02:27:26 +00001772 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001773 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1774 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1775}
1776
Reid Spencer7e933472006-12-12 00:49:44 +00001777CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1778 bool isSigned, const std::string &Name,
1779 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001780 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001781 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1782 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1783 Instruction::CastOps opcode =
1784 (SrcBits == DstBits ? Instruction::BitCast :
1785 (SrcBits > DstBits ? Instruction::Trunc :
1786 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1787 return create(opcode, C, Ty, Name, InsertBefore);
1788}
1789
1790CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1791 bool isSigned, const std::string &Name,
1792 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001793 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001794 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1795 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1796 Instruction::CastOps opcode =
1797 (SrcBits == DstBits ? Instruction::BitCast :
1798 (SrcBits > DstBits ? Instruction::Trunc :
1799 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1800 return create(opcode, C, Ty, Name, InsertAtEnd);
1801}
1802
1803CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1804 const std::string &Name,
1805 Instruction *InsertBefore) {
1806 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1807 "Invalid cast");
1808 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1809 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1810 Instruction::CastOps opcode =
1811 (SrcBits == DstBits ? Instruction::BitCast :
1812 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1813 return create(opcode, C, Ty, Name, InsertBefore);
1814}
1815
1816CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1817 const std::string &Name,
1818 BasicBlock *InsertAtEnd) {
1819 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1820 "Invalid cast");
1821 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1822 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1823 Instruction::CastOps opcode =
1824 (SrcBits == DstBits ? Instruction::BitCast :
1825 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1826 return create(opcode, C, Ty, Name, InsertAtEnd);
1827}
1828
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001829// Provide a way to get a "cast" where the cast opcode is inferred from the
1830// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001831// logic in the castIsValid function below. This axiom should hold:
1832// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1833// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001834// casting opcode for the arguments passed to it.
1835Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001836CastInst::getCastOpcode(
1837 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001838 // Get the bit sizes, we'll need these
1839 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001840 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1841 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001842
1843 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001844 if (DestTy->isInteger()) { // Casting to integral
1845 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846 if (DestBits < SrcBits)
1847 return Trunc; // int -> smaller int
1848 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001849 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001850 return SExt; // signed -> SEXT
1851 else
1852 return ZExt; // unsigned -> ZEXT
1853 } else {
1854 return BitCast; // Same size, No-op cast
1855 }
1856 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001857 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858 return FPToSI; // FP -> sint
1859 else
1860 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001861 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001862 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001863 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001864 return BitCast; // Same size, no-op cast
1865 } else {
1866 assert(isa<PointerType>(SrcTy) &&
1867 "Casting from a value that is not first-class type");
1868 return PtrToInt; // ptr -> int
1869 }
1870 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001871 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001872 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001873 return SIToFP; // sint -> FP
1874 else
1875 return UIToFP; // uint -> FP
1876 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1877 if (DestBits < SrcBits) {
1878 return FPTrunc; // FP -> smaller FP
1879 } else if (DestBits > SrcBits) {
1880 return FPExt; // FP -> larger FP
1881 } else {
1882 return BitCast; // same size, no-op cast
1883 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001884 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001885 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001886 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887 return BitCast; // same size, no-op cast
1888 } else {
1889 assert(0 && "Casting pointer or non-first class to float");
1890 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001891 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1892 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001894 "Casting vector to vector of different widths");
1895 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001896 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00001897 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00001899 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001900 }
1901 } else if (isa<PointerType>(DestTy)) {
1902 if (isa<PointerType>(SrcTy)) {
1903 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001904 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905 return IntToPtr; // int -> ptr
1906 } else {
1907 assert(!"Casting pointer to other than pointer or int");
1908 }
1909 } else {
1910 assert(!"Casting to type that is not first-class");
1911 }
1912
1913 // If we fall through to here we probably hit an assertion cast above
1914 // and assertions are not turned on. Anything we return is an error, so
1915 // BitCast is as good a choice as any.
1916 return BitCast;
1917}
1918
1919//===----------------------------------------------------------------------===//
1920// CastInst SubClass Constructors
1921//===----------------------------------------------------------------------===//
1922
1923/// Check that the construction parameters for a CastInst are correct. This
1924/// could be broken out into the separate constructors but it is useful to have
1925/// it in one place and to eliminate the redundant code for getting the sizes
1926/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001927bool
1928CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001929
1930 // Check for type sanity on the arguments
1931 const Type *SrcTy = S->getType();
1932 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1933 return false;
1934
1935 // Get the size of the types in bits, we'll need this later
1936 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1937 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1938
1939 // Switch on the opcode provided
1940 switch (op) {
1941 default: return false; // This is an input error
1942 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001943 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001944 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001945 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001946 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001947 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001948 case Instruction::FPTrunc:
1949 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1950 SrcBitSize > DstBitSize;
1951 case Instruction::FPExt:
1952 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1953 SrcBitSize < DstBitSize;
1954 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00001956 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1957 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1958 return SVTy->getElementType()->isInteger() &&
1959 DVTy->getElementType()->isFloatingPoint() &&
1960 SVTy->getNumElements() == DVTy->getNumElements();
1961 }
1962 }
Chris Lattner03c49532007-01-15 02:27:26 +00001963 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001964 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001965 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00001966 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1967 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1968 return SVTy->getElementType()->isFloatingPoint() &&
1969 DVTy->getElementType()->isInteger() &&
1970 SVTy->getNumElements() == DVTy->getNumElements();
1971 }
1972 }
Chris Lattner03c49532007-01-15 02:27:26 +00001973 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001974 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001975 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001976 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001977 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001978 case Instruction::BitCast:
1979 // BitCast implies a no-op cast of type only. No bits change.
1980 // However, you can't cast pointers to anything but pointers.
1981 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1982 return false;
1983
1984 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1985 // these cases, the cast is okay if the source and destination bit widths
1986 // are identical.
1987 return SrcBitSize == DstBitSize;
1988 }
1989}
1990
1991TruncInst::TruncInst(
1992 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1993) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001994 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001995}
1996
1997TruncInst::TruncInst(
1998 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1999) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002000 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002001}
2002
2003ZExtInst::ZExtInst(
2004 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2005) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002006 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007}
2008
2009ZExtInst::ZExtInst(
2010 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2011) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002012 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013}
2014SExtInst::SExtInst(
2015 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2016) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002017 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002018}
2019
Jeff Cohencc08c832006-12-02 02:22:01 +00002020SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2022) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002023 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002024}
2025
2026FPTruncInst::FPTruncInst(
2027 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2028) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002029 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030}
2031
2032FPTruncInst::FPTruncInst(
2033 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2034) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002035 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002036}
2037
2038FPExtInst::FPExtInst(
2039 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2040) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002041 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002042}
2043
2044FPExtInst::FPExtInst(
2045 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2046) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002047 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002048}
2049
2050UIToFPInst::UIToFPInst(
2051 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2052) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002053 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002054}
2055
2056UIToFPInst::UIToFPInst(
2057 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2058) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002059 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060}
2061
2062SIToFPInst::SIToFPInst(
2063 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2064) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002065 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002066}
2067
2068SIToFPInst::SIToFPInst(
2069 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2070) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002071 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072}
2073
2074FPToUIInst::FPToUIInst(
2075 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2076) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002077 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002078}
2079
2080FPToUIInst::FPToUIInst(
2081 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2082) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002083 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002084}
2085
2086FPToSIInst::FPToSIInst(
2087 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2088) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002089 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002090}
2091
2092FPToSIInst::FPToSIInst(
2093 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2094) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002095 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002096}
2097
2098PtrToIntInst::PtrToIntInst(
2099 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2100) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002101 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002102}
2103
2104PtrToIntInst::PtrToIntInst(
2105 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2106) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002107 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002108}
2109
2110IntToPtrInst::IntToPtrInst(
2111 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2112) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002113 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002114}
2115
2116IntToPtrInst::IntToPtrInst(
2117 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2118) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002119 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002120}
2121
2122BitCastInst::BitCastInst(
2123 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2124) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002125 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002126}
2127
2128BitCastInst::BitCastInst(
2129 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2130) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002131 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002132}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002133
2134//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002135// CmpInst Classes
2136//===----------------------------------------------------------------------===//
2137
2138CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2139 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002140 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002141 Ops[0].init(LHS, this);
2142 Ops[1].init(RHS, this);
2143 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002144 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002145 if (op == Instruction::ICmp) {
2146 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2147 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2148 "Invalid ICmp predicate value");
2149 const Type* Op0Ty = getOperand(0)->getType();
2150 const Type* Op1Ty = getOperand(1)->getType();
2151 assert(Op0Ty == Op1Ty &&
2152 "Both operands to ICmp instruction are not of the same type!");
2153 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002154 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002155 "Invalid operand types for ICmp instruction");
2156 return;
2157 }
2158 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2159 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2160 "Invalid FCmp predicate value");
2161 const Type* Op0Ty = getOperand(0)->getType();
2162 const Type* Op1Ty = getOperand(1)->getType();
2163 assert(Op0Ty == Op1Ty &&
2164 "Both operands to FCmp instruction are not of the same type!");
2165 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002166 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002167 "Invalid operand types for FCmp instruction");
2168}
2169
2170CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2171 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002172 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002173 Ops[0].init(LHS, this);
2174 Ops[1].init(RHS, this);
2175 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002176 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002177 if (op == Instruction::ICmp) {
2178 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2179 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2180 "Invalid ICmp predicate value");
2181
2182 const Type* Op0Ty = getOperand(0)->getType();
2183 const Type* Op1Ty = getOperand(1)->getType();
2184 assert(Op0Ty == Op1Ty &&
2185 "Both operands to ICmp instruction are not of the same type!");
2186 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002187 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002188 "Invalid operand types for ICmp instruction");
2189 return;
2190 }
2191 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2192 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2193 "Invalid FCmp predicate value");
2194 const Type* Op0Ty = getOperand(0)->getType();
2195 const Type* Op1Ty = getOperand(1)->getType();
2196 assert(Op0Ty == Op1Ty &&
2197 "Both operands to FCmp instruction are not of the same type!");
2198 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002199 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002200 "Invalid operand types for FCmp instruction");
2201}
2202
2203CmpInst *
2204CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2205 const std::string &Name, Instruction *InsertBefore) {
2206 if (Op == Instruction::ICmp) {
2207 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2208 InsertBefore);
2209 }
2210 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2211 InsertBefore);
2212}
2213
2214CmpInst *
2215CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2216 const std::string &Name, BasicBlock *InsertAtEnd) {
2217 if (Op == Instruction::ICmp) {
2218 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2219 InsertAtEnd);
2220 }
2221 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2222 InsertAtEnd);
2223}
2224
2225void CmpInst::swapOperands() {
2226 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2227 IC->swapOperands();
2228 else
2229 cast<FCmpInst>(this)->swapOperands();
2230}
2231
2232bool CmpInst::isCommutative() {
2233 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2234 return IC->isCommutative();
2235 return cast<FCmpInst>(this)->isCommutative();
2236}
2237
2238bool CmpInst::isEquality() {
2239 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2240 return IC->isEquality();
2241 return cast<FCmpInst>(this)->isEquality();
2242}
2243
2244
2245ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2246 switch (pred) {
2247 default:
2248 assert(!"Unknown icmp predicate!");
2249 case ICMP_EQ: return ICMP_NE;
2250 case ICMP_NE: return ICMP_EQ;
2251 case ICMP_UGT: return ICMP_ULE;
2252 case ICMP_ULT: return ICMP_UGE;
2253 case ICMP_UGE: return ICMP_ULT;
2254 case ICMP_ULE: return ICMP_UGT;
2255 case ICMP_SGT: return ICMP_SLE;
2256 case ICMP_SLT: return ICMP_SGE;
2257 case ICMP_SGE: return ICMP_SLT;
2258 case ICMP_SLE: return ICMP_SGT;
2259 }
2260}
2261
2262ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2263 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002264 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002265 case ICMP_EQ: case ICMP_NE:
2266 return pred;
2267 case ICMP_SGT: return ICMP_SLT;
2268 case ICMP_SLT: return ICMP_SGT;
2269 case ICMP_SGE: return ICMP_SLE;
2270 case ICMP_SLE: return ICMP_SGE;
2271 case ICMP_UGT: return ICMP_ULT;
2272 case ICMP_ULT: return ICMP_UGT;
2273 case ICMP_UGE: return ICMP_ULE;
2274 case ICMP_ULE: return ICMP_UGE;
2275 }
2276}
2277
Reid Spencer266e42b2006-12-23 06:05:41 +00002278ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2279 switch (pred) {
2280 default: assert(! "Unknown icmp predicate!");
2281 case ICMP_EQ: case ICMP_NE:
2282 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2283 return pred;
2284 case ICMP_UGT: return ICMP_SGT;
2285 case ICMP_ULT: return ICMP_SLT;
2286 case ICMP_UGE: return ICMP_SGE;
2287 case ICMP_ULE: return ICMP_SLE;
2288 }
2289}
2290
2291bool ICmpInst::isSignedPredicate(Predicate pred) {
2292 switch (pred) {
2293 default: assert(! "Unknown icmp predicate!");
2294 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2295 return true;
2296 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2297 case ICMP_UGE: case ICMP_ULE:
2298 return false;
2299 }
2300}
2301
Reid Spencer0286bc12007-02-28 22:00:54 +00002302/// Initialize a set of values that all satisfy the condition with C.
2303///
2304ConstantRange
2305ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2306 APInt Lower(C);
2307 APInt Upper(C);
2308 uint32_t BitWidth = C.getBitWidth();
2309 switch (pred) {
2310 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2311 case ICmpInst::ICMP_EQ: Upper++; break;
2312 case ICmpInst::ICMP_NE: Lower++; break;
2313 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2314 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2315 case ICmpInst::ICMP_UGT:
2316 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2317 break;
2318 case ICmpInst::ICMP_SGT:
2319 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2320 break;
2321 case ICmpInst::ICMP_ULE:
2322 Lower = APInt::getMinValue(BitWidth); Upper++;
2323 break;
2324 case ICmpInst::ICMP_SLE:
2325 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2326 break;
2327 case ICmpInst::ICMP_UGE:
2328 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2329 break;
2330 case ICmpInst::ICMP_SGE:
2331 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2332 break;
2333 }
2334 return ConstantRange(Lower, Upper);
2335}
2336
Reid Spencerd9436b62006-11-20 01:22:35 +00002337FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2338 switch (pred) {
2339 default:
2340 assert(!"Unknown icmp predicate!");
2341 case FCMP_OEQ: return FCMP_UNE;
2342 case FCMP_ONE: return FCMP_UEQ;
2343 case FCMP_OGT: return FCMP_ULE;
2344 case FCMP_OLT: return FCMP_UGE;
2345 case FCMP_OGE: return FCMP_ULT;
2346 case FCMP_OLE: return FCMP_UGT;
2347 case FCMP_UEQ: return FCMP_ONE;
2348 case FCMP_UNE: return FCMP_OEQ;
2349 case FCMP_UGT: return FCMP_OLE;
2350 case FCMP_ULT: return FCMP_OGE;
2351 case FCMP_UGE: return FCMP_OLT;
2352 case FCMP_ULE: return FCMP_OGT;
2353 case FCMP_ORD: return FCMP_UNO;
2354 case FCMP_UNO: return FCMP_ORD;
2355 case FCMP_TRUE: return FCMP_FALSE;
2356 case FCMP_FALSE: return FCMP_TRUE;
2357 }
2358}
2359
2360FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2361 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002362 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002363 case FCMP_FALSE: case FCMP_TRUE:
2364 case FCMP_OEQ: case FCMP_ONE:
2365 case FCMP_UEQ: case FCMP_UNE:
2366 case FCMP_ORD: case FCMP_UNO:
2367 return pred;
2368 case FCMP_OGT: return FCMP_OLT;
2369 case FCMP_OLT: return FCMP_OGT;
2370 case FCMP_OGE: return FCMP_OLE;
2371 case FCMP_OLE: return FCMP_OGE;
2372 case FCMP_UGT: return FCMP_ULT;
2373 case FCMP_ULT: return FCMP_UGT;
2374 case FCMP_UGE: return FCMP_ULE;
2375 case FCMP_ULE: return FCMP_UGE;
2376 }
2377}
2378
Reid Spencer266e42b2006-12-23 06:05:41 +00002379bool CmpInst::isUnsigned(unsigned short predicate) {
2380 switch (predicate) {
2381 default: return false;
2382 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2383 case ICmpInst::ICMP_UGE: return true;
2384 }
2385}
2386
2387bool CmpInst::isSigned(unsigned short predicate){
2388 switch (predicate) {
2389 default: return false;
2390 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2391 case ICmpInst::ICMP_SGE: return true;
2392 }
2393}
2394
2395bool CmpInst::isOrdered(unsigned short predicate) {
2396 switch (predicate) {
2397 default: return false;
2398 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2399 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2400 case FCmpInst::FCMP_ORD: return true;
2401 }
2402}
2403
2404bool CmpInst::isUnordered(unsigned short predicate) {
2405 switch (predicate) {
2406 default: return false;
2407 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2408 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2409 case FCmpInst::FCMP_UNO: return true;
2410 }
2411}
2412
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002413//===----------------------------------------------------------------------===//
2414// SwitchInst Implementation
2415//===----------------------------------------------------------------------===//
2416
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002417void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002418 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002419 ReservedSpace = 2+NumCases*2;
2420 NumOperands = 2;
2421 OperandList = new Use[ReservedSpace];
2422
2423 OperandList[0].init(Value, this);
2424 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002425}
2426
Chris Lattner2195fc42007-02-24 00:55:48 +00002427/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2428/// switch on and a default destination. The number of additional cases can
2429/// be specified here to make memory allocation more efficient. This
2430/// constructor can also autoinsert before another instruction.
2431SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2432 Instruction *InsertBefore)
2433 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2434 init(Value, Default, NumCases);
2435}
2436
2437/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2438/// switch on and a default destination. The number of additional cases can
2439/// be specified here to make memory allocation more efficient. This
2440/// constructor also autoinserts at the end of the specified BasicBlock.
2441SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2442 BasicBlock *InsertAtEnd)
2443 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2444 init(Value, Default, NumCases);
2445}
2446
Misha Brukmanb1c93172005-04-21 23:48:37 +00002447SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002448 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2449 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002450 Use *OL = OperandList, *InOL = SI.OperandList;
2451 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2452 OL[i].init(InOL[i], this);
2453 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002454 }
2455}
2456
Gordon Henriksen3e5be662007-12-09 22:46:10 +00002457void SwitchInst::destroyThis(SwitchInst*v) {
2458 delete [] v->OperandList;
2459 TerminatorInst::destroyThis(v);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002460}
2461
2462
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002463/// addCase - Add an entry to the switch instruction...
2464///
Chris Lattner47ac1872005-02-24 05:32:09 +00002465void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002466 unsigned OpNo = NumOperands;
2467 if (OpNo+2 > ReservedSpace)
2468 resizeOperands(0); // Get more space!
2469 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002470 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002471 NumOperands = OpNo+2;
2472 OperandList[OpNo].init(OnVal, this);
2473 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002474}
2475
2476/// removeCase - This method removes the specified successor from the switch
2477/// instruction. Note that this cannot be used to remove the default
2478/// destination (successor #0).
2479///
2480void SwitchInst::removeCase(unsigned idx) {
2481 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002482 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2483
2484 unsigned NumOps = getNumOperands();
2485 Use *OL = OperandList;
2486
2487 // Move everything after this operand down.
2488 //
2489 // FIXME: we could just swap with the end of the list, then erase. However,
2490 // client might not expect this to happen. The code as it is thrashes the
2491 // use/def lists, which is kinda lame.
2492 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2493 OL[i-2] = OL[i];
2494 OL[i-2+1] = OL[i+1];
2495 }
2496
2497 // Nuke the last value.
2498 OL[NumOps-2].set(0);
2499 OL[NumOps-2+1].set(0);
2500 NumOperands = NumOps-2;
2501}
2502
2503/// resizeOperands - resize operands - This adjusts the length of the operands
2504/// list according to the following behavior:
2505/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2506/// of operation. This grows the number of ops by 1.5 times.
2507/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2508/// 3. If NumOps == NumOperands, trim the reserved space.
2509///
2510void SwitchInst::resizeOperands(unsigned NumOps) {
2511 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002512 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002513 } else if (NumOps*2 > NumOperands) {
2514 // No resize needed.
2515 if (ReservedSpace >= NumOps) return;
2516 } else if (NumOps == NumOperands) {
2517 if (ReservedSpace == NumOps) return;
2518 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002519 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002520 }
2521
2522 ReservedSpace = NumOps;
2523 Use *NewOps = new Use[NumOps];
2524 Use *OldOps = OperandList;
2525 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2526 NewOps[i].init(OldOps[i], this);
2527 OldOps[i].set(0);
2528 }
2529 delete [] OldOps;
2530 OperandList = NewOps;
2531}
2532
2533
2534BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2535 return getSuccessor(idx);
2536}
2537unsigned SwitchInst::getNumSuccessorsV() const {
2538 return getNumSuccessors();
2539}
2540void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2541 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002542}
Chris Lattnerf22be932004-10-15 23:52:53 +00002543
2544
2545// Define these methods here so vtables don't get emitted into every translation
2546// unit that uses these classes.
2547
2548GetElementPtrInst *GetElementPtrInst::clone() const {
2549 return new GetElementPtrInst(*this);
2550}
2551
2552BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002553 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002554}
2555
Chris Lattner0b490b02007-08-24 20:48:18 +00002556FCmpInst* FCmpInst::clone() const {
2557 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2558}
2559ICmpInst* ICmpInst::clone() const {
2560 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002561}
2562
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2564AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2565FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2566LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2567StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2568CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2569CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2570CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2571CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2572CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2573CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2574CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2575CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2576CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2577CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2578CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2579CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2580CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2582VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2583
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002584ExtractElementInst *ExtractElementInst::clone() const {
2585 return new ExtractElementInst(*this);
2586}
2587InsertElementInst *InsertElementInst::clone() const {
2588 return new InsertElementInst(*this);
2589}
2590ShuffleVectorInst *ShuffleVectorInst::clone() const {
2591 return new ShuffleVectorInst(*this);
2592}
Chris Lattnerf22be932004-10-15 23:52:53 +00002593PHINode *PHINode::clone() const { return new PHINode(*this); }
2594ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2595BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2596SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2597InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2598UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002599UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}