blob: 39eebe67174a2f296b1a6396e17e2f70278c1417 [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"
20#include "llvm/Support/CallSite.h"
21using namespace llvm;
22
Chris Lattnerf7b6d312005-05-06 20:26:43 +000023unsigned CallSite::getCallingConv() const {
24 if (CallInst *CI = dyn_cast<CallInst>(I))
25 return CI->getCallingConv();
26 else
27 return cast<InvokeInst>(I)->getCallingConv();
28}
29void CallSite::setCallingConv(unsigned CC) {
30 if (CallInst *CI = dyn_cast<CallInst>(I))
31 CI->setCallingConv(CC);
32 else
33 cast<InvokeInst>(I)->setCallingConv(CC);
34}
35
36
Chris Lattner1c12a882006-06-21 16:53:47 +000037
38
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000039//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000040// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43TerminatorInst::TerminatorInst(Instruction::TermOps iType,
Misha Brukmanb1c93172005-04-21 23:48:37 +000044 Use *Ops, unsigned NumOps, Instruction *IB)
Chris Lattnerafdb3de2005-01-29 00:35:16 +000045 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
46}
47
48TerminatorInst::TerminatorInst(Instruction::TermOps iType,
49 Use *Ops, unsigned NumOps, BasicBlock *IAE)
50 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
51}
52
Chris Lattner1c12a882006-06-21 16:53:47 +000053// Out of line virtual method, so the vtable, etc has a home.
54TerminatorInst::~TerminatorInst() {
55}
56
57// Out of line virtual method, so the vtable, etc has a home.
58UnaryInstruction::~UnaryInstruction() {
59}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000060
61
62//===----------------------------------------------------------------------===//
63// PHINode Class
64//===----------------------------------------------------------------------===//
65
66PHINode::PHINode(const PHINode &PN)
67 : Instruction(PN.getType(), Instruction::PHI,
68 new Use[PN.getNumOperands()], PN.getNumOperands()),
69 ReservedSpace(PN.getNumOperands()) {
70 Use *OL = OperandList;
71 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
72 OL[i].init(PN.getOperand(i), this);
73 OL[i+1].init(PN.getOperand(i+1), this);
74 }
75}
76
77PHINode::~PHINode() {
78 delete [] OperandList;
79}
80
81// removeIncomingValue - Remove an incoming value. This is useful if a
82// predecessor basic block is deleted.
83Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
84 unsigned NumOps = getNumOperands();
85 Use *OL = OperandList;
86 assert(Idx*2 < NumOps && "BB not in PHI node!");
87 Value *Removed = OL[Idx*2];
88
89 // Move everything after this operand down.
90 //
91 // FIXME: we could just swap with the end of the list, then erase. However,
92 // client might not expect this to happen. The code as it is thrashes the
93 // use/def lists, which is kinda lame.
94 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
95 OL[i-2] = OL[i];
96 OL[i-2+1] = OL[i+1];
97 }
98
99 // Nuke the last value.
100 OL[NumOps-2].set(0);
101 OL[NumOps-2+1].set(0);
102 NumOperands = NumOps-2;
103
104 // If the PHI node is dead, because it has zero entries, nuke it now.
105 if (NumOps == 2 && DeletePHIIfEmpty) {
106 // If anyone is using this PHI, make them use a dummy value instead...
107 replaceAllUsesWith(UndefValue::get(getType()));
108 eraseFromParent();
109 }
110 return Removed;
111}
112
113/// resizeOperands - resize operands - This adjusts the length of the operands
114/// list according to the following behavior:
115/// 1. If NumOps == 0, grow the operand list in response to a push_back style
116/// of operation. This grows the number of ops by 1.5 times.
117/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
118/// 3. If NumOps == NumOperands, trim the reserved space.
119///
120void PHINode::resizeOperands(unsigned NumOps) {
121 if (NumOps == 0) {
122 NumOps = (getNumOperands())*3/2;
123 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
124 } else if (NumOps*2 > NumOperands) {
125 // No resize needed.
126 if (ReservedSpace >= NumOps) return;
127 } else if (NumOps == NumOperands) {
128 if (ReservedSpace == NumOps) return;
129 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 }
132
133 ReservedSpace = NumOps;
134 Use *NewOps = new Use[NumOps];
135 Use *OldOps = OperandList;
136 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
137 NewOps[i].init(OldOps[i], this);
138 OldOps[i].set(0);
139 }
140 delete [] OldOps;
141 OperandList = NewOps;
142}
143
Nate Begemanb3923212005-08-04 23:24:19 +0000144/// hasConstantValue - If the specified PHI node always merges together the same
145/// value, return the value, otherwise return null.
146///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000147Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000148 // If the PHI node only has one incoming value, eliminate the PHI node...
149 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000150 if (getIncomingValue(0) != this) // not X = phi X
151 return getIncomingValue(0);
152 else
153 return UndefValue::get(getType()); // Self cycle is dead.
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155 // Otherwise if all of the incoming values are the same for the PHI, replace
156 // the PHI node with the incoming value.
157 //
158 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000159 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000160 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000161 if (isa<UndefValue>(getIncomingValue(i)))
162 HasUndefInput = true;
163 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000164 if (InVal && getIncomingValue(i) != InVal)
165 return 0; // Not the same, bail out.
166 else
167 InVal = getIncomingValue(i);
168
169 // The only case that could cause InVal to be null is if we have a PHI node
170 // that only has entries for itself. In this case, there is no entry into the
171 // loop, so kill the PHI.
172 //
173 if (InVal == 0) InVal = UndefValue::get(getType());
174
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 // If we have a PHI node like phi(X, undef, X), where X is defined by some
176 // instruction, we cannot always return X as the result of the PHI node. Only
177 // do this if X is not an instruction (thus it must dominate the PHI block),
178 // or if the client is prepared to deal with this possibility.
179 if (HasUndefInput && !AllowNonDominatingInstruction)
180 if (Instruction *IV = dyn_cast<Instruction>(InVal))
181 // If it's in the entry block, it dominates everything.
Chris Lattner37774af2005-08-05 01:03:27 +0000182 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
183 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000184 return 0; // Cannot guarantee that InVal dominates this PHINode.
185
Nate Begemanb3923212005-08-04 23:24:19 +0000186 // All of the incoming values are the same, return the value now.
187 return InVal;
188}
189
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190
191//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000192// CallInst Implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195CallInst::~CallInst() {
196 delete [] OperandList;
197}
198
199void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
200 NumOperands = Params.size()+1;
201 Use *OL = OperandList = new Use[Params.size()+1];
202 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203
Misha Brukmanb1c93172005-04-21 23:48:37 +0000204 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000205 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
206
Misha Brukmanb1c93172005-04-21 23:48:37 +0000207 assert((Params.size() == FTy->getNumParams() ||
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000208 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000209 "Calling a function with bad signature!");
210 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
211 assert((i >= FTy->getNumParams() ||
212 FTy->getParamType(i) == Params[i]->getType()) &&
213 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000214 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000215 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000216}
217
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
219 NumOperands = 3;
220 Use *OL = OperandList = new Use[3];
221 OL[0].init(Func, this);
222 OL[1].init(Actual1, this);
223 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000224
225 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
227
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000229 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000230 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000231 assert((0 >= FTy->getNumParams() ||
232 FTy->getParamType(0) == Actual1->getType()) &&
233 "Calling a function with a bad signature!");
234 assert((1 >= FTy->getNumParams() ||
235 FTy->getParamType(1) == Actual2->getType()) &&
236 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237}
238
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000239void CallInst::init(Value *Func, Value *Actual) {
240 NumOperands = 2;
241 Use *OL = OperandList = new Use[2];
242 OL[0].init(Func, this);
243 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000244
245 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
247
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000248 assert((FTy->getNumParams() == 1 ||
249 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000251 assert((0 == FTy->getNumParams() ||
252 FTy->getParamType(0) == Actual->getType()) &&
253 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254}
255
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000256void CallInst::init(Value *Func) {
257 NumOperands = 1;
258 Use *OL = OperandList = new Use[1];
259 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000260
261 const FunctionType *MTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
263
264 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
265}
266
Misha Brukmanb1c93172005-04-21 23:48:37 +0000267CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
268 const std::string &Name, Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
270 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000271 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272 init(Func, Params);
273}
274
Misha Brukmanb1c93172005-04-21 23:48:37 +0000275CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
276 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
278 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 init(Func, Params);
281}
282
283CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
284 const std::string &Name, Instruction *InsertBefore)
285 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
286 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000287 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288 init(Func, Actual1, Actual2);
289}
290
291CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
292 const std::string &Name, BasicBlock *InsertAtEnd)
293 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
294 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000295 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296 init(Func, Actual1, Actual2);
297}
298
299CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
300 Instruction *InsertBefore)
301 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
302 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000303 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304 init(Func, Actual);
305}
306
307CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
308 BasicBlock *InsertAtEnd)
309 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
310 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000311 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 init(Func, Actual);
313}
314
315CallInst::CallInst(Value *Func, const std::string &Name,
316 Instruction *InsertBefore)
317 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
318 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000319 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 init(Func);
321}
322
323CallInst::CallInst(Value *Func, const std::string &Name,
324 BasicBlock *InsertAtEnd)
325 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
326 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000327 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 init(Func);
329}
330
Misha Brukmanb1c93172005-04-21 23:48:37 +0000331CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000332 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
333 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000334 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000335 Use *OL = OperandList;
336 Use *InOL = CI.OperandList;
337 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
338 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339}
340
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341
342//===----------------------------------------------------------------------===//
343// InvokeInst Implementation
344//===----------------------------------------------------------------------===//
345
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000346InvokeInst::~InvokeInst() {
347 delete [] OperandList;
348}
349
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000351 const std::vector<Value*> &Params) {
352 NumOperands = 3+Params.size();
353 Use *OL = OperandList = new Use[3+Params.size()];
354 OL[0].init(Fn, this);
355 OL[1].init(IfNormal, this);
356 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000357 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000359
360 assert((Params.size() == FTy->getNumParams()) ||
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000361 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000363
Chris Lattner667a0562006-05-03 00:48:22 +0000364 for (unsigned i = 0, e = Params.size(); i != e; i++) {
365 assert((i >= FTy->getNumParams() ||
366 FTy->getParamType(i) == Params[i]->getType()) &&
367 "Invoking a function with a bad signature!");
368
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000369 OL[i+3].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000370 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371}
372
373InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
374 BasicBlock *IfException,
375 const std::vector<Value*> &Params,
376 const std::string &Name, Instruction *InsertBefore)
377 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
378 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000379 Instruction::Invoke, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380 init(Fn, IfNormal, IfException, Params);
381}
382
383InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
384 BasicBlock *IfException,
385 const std::vector<Value*> &Params,
386 const std::string &Name, BasicBlock *InsertAtEnd)
387 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
388 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000389 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000390 init(Fn, IfNormal, IfException, Params);
391}
392
Misha Brukmanb1c93172005-04-21 23:48:37 +0000393InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000394 : TerminatorInst(II.getType(), Instruction::Invoke,
395 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000396 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000397 Use *OL = OperandList, *InOL = II.OperandList;
398 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
399 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000400}
401
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
403 return getSuccessor(idx);
404}
405unsigned InvokeInst::getNumSuccessorsV() const {
406 return getNumSuccessors();
407}
408void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
409 return setSuccessor(idx, B);
410}
411
412
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000413//===----------------------------------------------------------------------===//
414// ReturnInst Implementation
415//===----------------------------------------------------------------------===//
416
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000417void ReturnInst::init(Value *retVal) {
418 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000419 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000420 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000421 NumOperands = 1;
422 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000423 }
424}
425
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000426unsigned ReturnInst::getNumSuccessorsV() const {
427 return getNumSuccessors();
428}
429
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000430// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
431// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000432void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000433 assert(0 && "ReturnInst has no successors!");
434}
435
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000436BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
437 assert(0 && "ReturnInst has no successors!");
438 abort();
439 return 0;
440}
441
442
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443//===----------------------------------------------------------------------===//
444// UnwindInst Implementation
445//===----------------------------------------------------------------------===//
446
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000447unsigned UnwindInst::getNumSuccessorsV() const {
448 return getNumSuccessors();
449}
450
451void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000452 assert(0 && "UnwindInst has no successors!");
453}
454
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000455BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
456 assert(0 && "UnwindInst has no successors!");
457 abort();
458 return 0;
459}
460
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000461//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000462// UnreachableInst Implementation
463//===----------------------------------------------------------------------===//
464
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000465unsigned UnreachableInst::getNumSuccessorsV() const {
466 return getNumSuccessors();
467}
468
469void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
470 assert(0 && "UnwindInst has no successors!");
471}
472
473BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
474 assert(0 && "UnwindInst has no successors!");
475 abort();
476 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000477}
478
479//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000480// BranchInst Implementation
481//===----------------------------------------------------------------------===//
482
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000483void BranchInst::AssertOK() {
484 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000485 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000486 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000487}
488
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000489BranchInst::BranchInst(const BranchInst &BI) :
490 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
491 OperandList[0].init(BI.getOperand(0), this);
492 if (BI.getNumOperands() != 1) {
493 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
494 OperandList[1].init(BI.getOperand(1), this);
495 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000496 }
497}
498
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000499BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
500 return getSuccessor(idx);
501}
502unsigned BranchInst::getNumSuccessorsV() const {
503 return getNumSuccessors();
504}
505void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
506 setSuccessor(idx, B);
507}
508
509
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000510//===----------------------------------------------------------------------===//
511// AllocationInst Implementation
512//===----------------------------------------------------------------------===//
513
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000514static Value *getAISize(Value *Amt) {
515 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000516 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000517 else {
518 assert(!isa<BasicBlock>(Amt) &&
519 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000520 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000521 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000522 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000523 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000524}
525
Misha Brukmanb1c93172005-04-21 23:48:37 +0000526AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000527 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000529 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000530 Name, InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000531 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000533}
534
Misha Brukmanb1c93172005-04-21 23:48:37 +0000535AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000536 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000537 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000538 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000539 Name, InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000540 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000541 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542}
543
Chris Lattner1c12a882006-06-21 16:53:47 +0000544// Out of line virtual method, so the vtable, etc has a home.
545AllocationInst::~AllocationInst() {
546}
547
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000548bool AllocationInst::isArrayAllocation() const {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000549 if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0)))
550 return CUI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000551 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000552}
553
554const Type *AllocationInst::getAllocatedType() const {
555 return getType()->getElementType();
556}
557
558AllocaInst::AllocaInst(const AllocaInst &AI)
559 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000560 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000561}
562
563MallocInst::MallocInst(const MallocInst &MI)
564 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000565 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566}
567
568//===----------------------------------------------------------------------===//
569// FreeInst Implementation
570//===----------------------------------------------------------------------===//
571
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572void FreeInst::AssertOK() {
573 assert(isa<PointerType>(getOperand(0)->getType()) &&
574 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575}
576
577FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
579 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000580}
581
582FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000583 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
584 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000585}
586
587
588//===----------------------------------------------------------------------===//
589// LoadInst Implementation
590//===----------------------------------------------------------------------===//
591
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000593 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000594 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000595}
596
597LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000598 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000599 Load, Ptr, Name, InsertBef) {
600 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000601 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000602}
603
604LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000606 Load, Ptr, Name, InsertAE) {
607 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609}
610
611LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
612 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000614 Load, Ptr, Name, InsertBef) {
615 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000616 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000617}
618
619LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
620 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000621 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000622 Load, Ptr, Name, InsertAE) {
623 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000625}
626
627
628//===----------------------------------------------------------------------===//
629// StoreInst Implementation
630//===----------------------------------------------------------------------===//
631
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632void StoreInst::AssertOK() {
633 assert(isa<PointerType>(getOperand(1)->getType()) &&
634 "Ptr must have pointer type!");
635 assert(getOperand(0)->getType() ==
636 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000637 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000640
641StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000642 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000643 Ops[0].init(val, this);
644 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000645 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000646 AssertOK();
647}
648
649StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000650 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651 Ops[0].init(val, this);
652 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000653 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654 AssertOK();
655}
656
Misha Brukmanb1c93172005-04-21 23:48:37 +0000657StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000659 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660 Ops[0].init(val, this);
661 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000662 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663 AssertOK();
664}
665
Misha Brukmanb1c93172005-04-21 23:48:37 +0000666StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000667 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000668 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669 Ops[0].init(val, this);
670 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000671 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000673}
674
675//===----------------------------------------------------------------------===//
676// GetElementPtrInst Implementation
677//===----------------------------------------------------------------------===//
678
679// checkType - Simple wrapper function to give a better assertion failure
680// message on bad indexes for a gep instruction.
681//
682static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000683 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684 return Ty;
685}
686
Chris Lattner79807c3d2007-01-31 19:47:18 +0000687void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
688 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000689 Use *OL = OperandList = new Use[NumOperands];
690 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000691
Chris Lattner79807c3d2007-01-31 19:47:18 +0000692 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694}
695
696void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697 NumOperands = 3;
698 Use *OL = OperandList = new Use[3];
699 OL[0].init(Ptr, this);
700 OL[1].init(Idx0, this);
701 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702}
703
Chris Lattner82981202005-05-03 05:43:30 +0000704void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
705 NumOperands = 2;
706 Use *OL = OperandList = new Use[2];
707 OL[0].init(Ptr, this);
708 OL[1].init(Idx, this);
709}
710
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000711GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000712 const std::string &Name, Instruction *InBe)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000714 &Idx[0], Idx.size(),
715 true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716 GetElementPtr, 0, 0, Name, InBe) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000717 init(Ptr, &Idx[0], Idx.size());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718}
719
720GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000721 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000723 &Idx[0], Idx.size(),
724 true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 GetElementPtr, 0, 0, Name, IAE) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000726 init(Ptr, &Idx[0], Idx.size());
727}
728
729GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
730 unsigned NumIdx,
731 const std::string &Name, Instruction *InBe)
732: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000733 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000734 GetElementPtr, 0, 0, Name, InBe) {
735 init(Ptr, Idx, NumIdx);
736}
737
738GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
739 unsigned NumIdx,
740 const std::string &Name, BasicBlock *IAE)
741: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000742 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000743 GetElementPtr, 0, 0, Name, IAE) {
744 init(Ptr, Idx, NumIdx);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000745}
746
Chris Lattner82981202005-05-03 05:43:30 +0000747GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
748 const std::string &Name, Instruction *InBe)
Reid Spencerdee14b52007-01-31 22:30:26 +0000749 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
750 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000751 GetElementPtr, 0, 0, Name, InBe) {
752 init(Ptr, Idx);
753}
754
755GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
756 const std::string &Name, BasicBlock *IAE)
Reid Spencerdee14b52007-01-31 22:30:26 +0000757 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
758 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000759 GetElementPtr, 0, 0, Name, IAE) {
760 init(Ptr, Idx);
761}
762
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000763GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
764 const std::string &Name, Instruction *InBe)
765 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
766 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000767 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000768 init(Ptr, Idx0, Idx1);
769}
770
771GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000772 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000773 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
774 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776 init(Ptr, Idx0, Idx1);
777}
778
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779GetElementPtrInst::~GetElementPtrInst() {
780 delete[] OperandList;
781}
782
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000783// getIndexedType - Returns the type of the element that would be loaded with
784// a load instruction with the specified parameters.
785//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000786// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787// pointer type.
788//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000789const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000790 Value* const *Idxs,
791 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792 bool AllowCompositeLeaf) {
793 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
794
795 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000796 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797 if (AllowCompositeLeaf ||
798 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
799 return cast<PointerType>(Ptr)->getElementType();
800 else
801 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000802
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803 unsigned CurIdx = 0;
804 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000805 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000806 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
807 return 0; // Can't load a whole structure or array!?!?
808 }
809
Chris Lattner302116a2007-01-31 04:40:28 +0000810 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811 if (isa<PointerType>(CT) && CurIdx != 1)
812 return 0; // Can only index into pointer types at the first index!
813 if (!CT->indexValid(Index)) return 0;
814 Ptr = CT->getTypeAtIndex(Index);
815
816 // If the new type forwards to another type, then it is in the middle
817 // of being refined to another type (and hence, may have dropped all
818 // references to what it was using before). So, use the new forwarded
819 // type.
820 if (const Type * Ty = Ptr->getForwardedType()) {
821 Ptr = Ty;
822 }
823 }
Chris Lattner302116a2007-01-31 04:40:28 +0000824 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000825}
826
Misha Brukmanb1c93172005-04-21 23:48:37 +0000827const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828 Value *Idx0, Value *Idx1,
829 bool AllowCompositeLeaf) {
830 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
831 if (!PTy) return 0; // Type isn't a pointer type!
832
833 // Check the pointer index.
834 if (!PTy->indexValid(Idx0)) return 0;
835
836 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
837 if (!CT || !CT->indexValid(Idx1)) return 0;
838
839 const Type *ElTy = CT->getTypeAtIndex(Idx1);
840 if (AllowCompositeLeaf || ElTy->isFirstClassType())
841 return ElTy;
842 return 0;
843}
844
Chris Lattner82981202005-05-03 05:43:30 +0000845const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
846 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
847 if (!PTy) return 0; // Type isn't a pointer type!
848
849 // Check the pointer index.
850 if (!PTy->indexValid(Idx)) return 0;
851
Chris Lattnerc2233332005-05-03 16:44:45 +0000852 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000853}
854
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000855//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000856// ExtractElementInst Implementation
857//===----------------------------------------------------------------------===//
858
859ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000860 const std::string &Name,
861 Instruction *InsertBef)
Robert Bocchino23004482006-01-10 19:05:34 +0000862 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
863 ExtractElement, Ops, 2, Name, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000864 assert(isValidOperands(Val, Index) &&
865 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000866 Ops[0].init(Val, this);
867 Ops[1].init(Index, this);
868}
869
Chris Lattner65511ff2006-10-05 06:24:58 +0000870ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
871 const std::string &Name,
872 Instruction *InsertBef)
873 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
874 ExtractElement, Ops, 2, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000875 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000876 assert(isValidOperands(Val, Index) &&
877 "Invalid extractelement instruction operands!");
878 Ops[0].init(Val, this);
879 Ops[1].init(Index, this);
880}
881
882
Robert Bocchino23004482006-01-10 19:05:34 +0000883ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000884 const std::string &Name,
885 BasicBlock *InsertAE)
Robert Bocchino23004482006-01-10 19:05:34 +0000886 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
887 ExtractElement, Ops, 2, Name, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +0000888 assert(isValidOperands(Val, Index) &&
889 "Invalid extractelement instruction operands!");
890
Robert Bocchino23004482006-01-10 19:05:34 +0000891 Ops[0].init(Val, this);
892 Ops[1].init(Index, this);
893}
894
Chris Lattner65511ff2006-10-05 06:24:58 +0000895ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
896 const std::string &Name,
897 BasicBlock *InsertAE)
898 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
899 ExtractElement, Ops, 2, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000900 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000901 assert(isValidOperands(Val, Index) &&
902 "Invalid extractelement instruction operands!");
903
904 Ops[0].init(Val, this);
905 Ops[1].init(Index, this);
906}
907
908
Chris Lattner54865b32006-04-08 04:05:48 +0000909bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000910 if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000911 return false;
912 return true;
913}
914
915
Robert Bocchino23004482006-01-10 19:05:34 +0000916//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +0000917// InsertElementInst Implementation
918//===----------------------------------------------------------------------===//
919
Chris Lattner0875d942006-04-14 22:20:32 +0000920InsertElementInst::InsertElementInst(const InsertElementInst &IE)
921 : Instruction(IE.getType(), InsertElement, Ops, 3) {
922 Ops[0].init(IE.Ops[0], this);
923 Ops[1].init(IE.Ops[1], this);
924 Ops[2].init(IE.Ops[2], this);
925}
Chris Lattner54865b32006-04-08 04:05:48 +0000926InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000927 const std::string &Name,
928 Instruction *InsertBef)
Chris Lattner54865b32006-04-08 04:05:48 +0000929 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
930 assert(isValidOperands(Vec, Elt, Index) &&
931 "Invalid insertelement instruction operands!");
932 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000933 Ops[1].init(Elt, this);
934 Ops[2].init(Index, this);
935}
936
Chris Lattner65511ff2006-10-05 06:24:58 +0000937InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
938 const std::string &Name,
939 Instruction *InsertBef)
940 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000941 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000942 assert(isValidOperands(Vec, Elt, Index) &&
943 "Invalid insertelement instruction operands!");
944 Ops[0].init(Vec, this);
945 Ops[1].init(Elt, this);
946 Ops[2].init(Index, this);
947}
948
949
Chris Lattner54865b32006-04-08 04:05:48 +0000950InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000951 const std::string &Name,
952 BasicBlock *InsertAE)
Chris Lattner54865b32006-04-08 04:05:48 +0000953 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
954 assert(isValidOperands(Vec, Elt, Index) &&
955 "Invalid insertelement instruction operands!");
956
957 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000958 Ops[1].init(Elt, this);
959 Ops[2].init(Index, this);
960}
961
Chris Lattner65511ff2006-10-05 06:24:58 +0000962InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
963 const std::string &Name,
964 BasicBlock *InsertAE)
965: Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000966 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000967 assert(isValidOperands(Vec, Elt, Index) &&
968 "Invalid insertelement instruction operands!");
969
970 Ops[0].init(Vec, this);
971 Ops[1].init(Elt, this);
972 Ops[2].init(Index, this);
973}
974
Chris Lattner54865b32006-04-08 04:05:48 +0000975bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
976 const Value *Index) {
977 if (!isa<PackedType>(Vec->getType()))
978 return false; // First operand of insertelement must be packed type.
979
980 if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType())
981 return false;// Second operand of insertelement must be packed element type.
982
Reid Spencer8d9336d2006-12-31 05:26:44 +0000983 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000984 return false; // Third operand of insertelement must be uint.
985 return true;
986}
987
988
Robert Bocchinoca27f032006-01-17 20:07:22 +0000989//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000990// ShuffleVectorInst Implementation
991//===----------------------------------------------------------------------===//
992
Chris Lattner0875d942006-04-14 22:20:32 +0000993ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
994 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
995 Ops[0].init(SV.Ops[0], this);
996 Ops[1].init(SV.Ops[1], this);
997 Ops[2].init(SV.Ops[2], this);
998}
999
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001000ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1001 const std::string &Name,
1002 Instruction *InsertBefore)
1003 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) {
1004 assert(isValidOperands(V1, V2, Mask) &&
1005 "Invalid shuffle vector instruction operands!");
1006 Ops[0].init(V1, this);
1007 Ops[1].init(V2, this);
1008 Ops[2].init(Mask, this);
1009}
1010
1011ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1012 const std::string &Name,
1013 BasicBlock *InsertAtEnd)
1014 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) {
1015 assert(isValidOperands(V1, V2, Mask) &&
1016 "Invalid shuffle vector instruction operands!");
1017
1018 Ops[0].init(V1, this);
1019 Ops[1].init(V2, this);
1020 Ops[2].init(Mask, this);
1021}
1022
1023bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1024 const Value *Mask) {
1025 if (!isa<PackedType>(V1->getType())) return false;
1026 if (V1->getType() != V2->getType()) return false;
1027 if (!isa<PackedType>(Mask->getType()) ||
Reid Spencer8d9336d2006-12-31 05:26:44 +00001028 cast<PackedType>(Mask->getType())->getElementType() != Type::Int32Ty ||
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001029 cast<PackedType>(Mask->getType())->getNumElements() !=
1030 cast<PackedType>(V1->getType())->getNumElements())
1031 return false;
1032 return true;
1033}
1034
1035
1036//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037// BinaryOperator Class
1038//===----------------------------------------------------------------------===//
1039
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001040void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041{
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001042 Value *LHS = getOperand(0), *RHS = getOperand(1);
1043 assert(LHS->getType() == RHS->getType() &&
1044 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001045#ifndef NDEBUG
1046 switch (iType) {
1047 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001048 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001049 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001051 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001052 isa<PackedType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001053 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001054 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001055 case UDiv:
1056 case SDiv:
1057 assert(getType() == LHS->getType() &&
1058 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001059 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1060 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001061 "Incorrect operand type (not integer) for S/UDIV");
1062 break;
1063 case FDiv:
1064 assert(getType() == LHS->getType() &&
1065 "Arithmetic operation should return same type as operands!");
1066 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1067 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1068 && "Incorrect operand type (not floating point) for FDIV");
1069 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001070 case URem:
1071 case SRem:
1072 assert(getType() == LHS->getType() &&
1073 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001074 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1075 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001076 "Incorrect operand type (not integer) for S/UREM");
1077 break;
1078 case FRem:
1079 assert(getType() == LHS->getType() &&
1080 "Arithmetic operation should return same type as operands!");
1081 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1082 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1083 && "Incorrect operand type (not floating point) for FREM");
1084 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001085 case And: case Or:
1086 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001087 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001088 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001089 assert((getType()->isInteger() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001090 (isa<PackedType>(getType()) &&
Chris Lattner03c49532007-01-15 02:27:26 +00001091 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001092 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001093 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001094 default:
1095 break;
1096 }
1097#endif
1098}
1099
1100BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001101 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001102 Instruction *InsertBefore) {
1103 assert(S1->getType() == S2->getType() &&
1104 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001105 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001106}
1107
1108BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001109 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001110 BasicBlock *InsertAtEnd) {
1111 BinaryOperator *Res = create(Op, S1, S2, Name);
1112 InsertAtEnd->getInstList().push_back(Res);
1113 return Res;
1114}
1115
1116BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1117 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001118 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1119 return new BinaryOperator(Instruction::Sub,
1120 zero, Op,
1121 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001122}
1123
1124BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1125 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001126 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1127 return new BinaryOperator(Instruction::Sub,
1128 zero, Op,
1129 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001130}
1131
1132BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1133 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001134 Constant *C;
1135 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001136 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001137 C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C));
1138 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001139 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001140 }
1141
1142 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143 Op->getType(), Name, InsertBefore);
1144}
1145
1146BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1147 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001148 Constant *AllOnes;
1149 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
1150 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001151 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001152 AllOnes =
1153 ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1154 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001155 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001156 }
1157
1158 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001159 Op->getType(), Name, InsertAtEnd);
1160}
1161
1162
1163// isConstantAllOnes - Helper function for several functions below
1164static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001165 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001166}
1167
1168bool BinaryOperator::isNeg(const Value *V) {
1169 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1170 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001171 return Bop->getOperand(0) ==
1172 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001173 return false;
1174}
1175
1176bool BinaryOperator::isNot(const Value *V) {
1177 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1178 return (Bop->getOpcode() == Instruction::Xor &&
1179 (isConstantAllOnes(Bop->getOperand(1)) ||
1180 isConstantAllOnes(Bop->getOperand(0))));
1181 return false;
1182}
1183
Chris Lattner2c7d1772005-04-24 07:28:37 +00001184Value *BinaryOperator::getNegArgument(Value *BinOp) {
1185 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1186 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001187}
1188
Chris Lattner2c7d1772005-04-24 07:28:37 +00001189const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1190 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001191}
1192
Chris Lattner2c7d1772005-04-24 07:28:37 +00001193Value *BinaryOperator::getNotArgument(Value *BinOp) {
1194 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1195 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1196 Value *Op0 = BO->getOperand(0);
1197 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001198 if (isConstantAllOnes(Op0)) return Op1;
1199
1200 assert(isConstantAllOnes(Op1));
1201 return Op0;
1202}
1203
Chris Lattner2c7d1772005-04-24 07:28:37 +00001204const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1205 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001206}
1207
1208
1209// swapOperands - Exchange the two operands to this instruction. This
1210// instruction is safe to use on any binary instruction and does not
1211// modify the semantics of the instruction. If the instruction is
1212// order dependent (SetLT f.e.) the opcode is changed.
1213//
1214bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001215 if (!isCommutative())
1216 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001217 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001218 return false;
1219}
1220
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001221//===----------------------------------------------------------------------===//
1222// CastInst Class
1223//===----------------------------------------------------------------------===//
1224
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001225// Just determine if this cast only deals with integral->integral conversion.
1226bool CastInst::isIntegerCast() const {
1227 switch (getOpcode()) {
1228 default: return false;
1229 case Instruction::ZExt:
1230 case Instruction::SExt:
1231 case Instruction::Trunc:
1232 return true;
1233 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001234 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001235 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001236}
1237
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001238bool CastInst::isLosslessCast() const {
1239 // Only BitCast can be lossless, exit fast if we're not BitCast
1240 if (getOpcode() != Instruction::BitCast)
1241 return false;
1242
1243 // Identity cast is always lossless
1244 const Type* SrcTy = getOperand(0)->getType();
1245 const Type* DstTy = getType();
1246 if (SrcTy == DstTy)
1247 return true;
1248
Reid Spencer8d9336d2006-12-31 05:26:44 +00001249 // Pointer to pointer is always lossless.
1250 if (isa<PointerType>(SrcTy))
1251 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001252 return false; // Other types have no identity values
1253}
1254
1255/// This function determines if the CastInst does not require any bits to be
1256/// changed in order to effect the cast. Essentially, it identifies cases where
1257/// no code gen is necessary for the cast, hence the name no-op cast. For
1258/// example, the following are all no-op casts:
1259/// # bitcast uint %X, int
1260/// # bitcast uint* %x, sbyte*
1261/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1262/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1263/// @brief Determine if a cast is a no-op.
1264bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1265 switch (getOpcode()) {
1266 default:
1267 assert(!"Invalid CastOp");
1268 case Instruction::Trunc:
1269 case Instruction::ZExt:
1270 case Instruction::SExt:
1271 case Instruction::FPTrunc:
1272 case Instruction::FPExt:
1273 case Instruction::UIToFP:
1274 case Instruction::SIToFP:
1275 case Instruction::FPToUI:
1276 case Instruction::FPToSI:
1277 return false; // These always modify bits
1278 case Instruction::BitCast:
1279 return true; // BitCast never modifies bits.
1280 case Instruction::PtrToInt:
1281 return IntPtrTy->getPrimitiveSizeInBits() ==
1282 getType()->getPrimitiveSizeInBits();
1283 case Instruction::IntToPtr:
1284 return IntPtrTy->getPrimitiveSizeInBits() ==
1285 getOperand(0)->getType()->getPrimitiveSizeInBits();
1286 }
1287}
1288
1289/// This function determines if a pair of casts can be eliminated and what
1290/// opcode should be used in the elimination. This assumes that there are two
1291/// instructions like this:
1292/// * %F = firstOpcode SrcTy %x to MidTy
1293/// * %S = secondOpcode MidTy %F to DstTy
1294/// The function returns a resultOpcode so these two casts can be replaced with:
1295/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1296/// If no such cast is permited, the function returns 0.
1297unsigned CastInst::isEliminableCastPair(
1298 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1299 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1300{
1301 // Define the 144 possibilities for these two cast instructions. The values
1302 // in this matrix determine what to do in a given situation and select the
1303 // case in the switch below. The rows correspond to firstOp, the columns
1304 // correspond to secondOp. In looking at the table below, keep in mind
1305 // the following cast properties:
1306 //
1307 // Size Compare Source Destination
1308 // Operator Src ? Size Type Sign Type Sign
1309 // -------- ------------ ------------------- ---------------------
1310 // TRUNC > Integer Any Integral Any
1311 // ZEXT < Integral Unsigned Integer Any
1312 // SEXT < Integral Signed Integer Any
1313 // FPTOUI n/a FloatPt n/a Integral Unsigned
1314 // FPTOSI n/a FloatPt n/a Integral Signed
1315 // UITOFP n/a Integral Unsigned FloatPt n/a
1316 // SITOFP n/a Integral Signed FloatPt n/a
1317 // FPTRUNC > FloatPt n/a FloatPt n/a
1318 // FPEXT < FloatPt n/a FloatPt n/a
1319 // PTRTOINT n/a Pointer n/a Integral Unsigned
1320 // INTTOPTR n/a Integral Unsigned Pointer n/a
1321 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001322 //
1323 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1324 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1325 // into "fptoui double to ulong", but this loses information about the range
1326 // of the produced value (we no longer know the top-part is all zeros).
1327 // Further this conversion is often much more expensive for typical hardware,
1328 // and causes issues when building libgcc. We disallow fptosi+sext for the
1329 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001330 const unsigned numCastOps =
1331 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1332 static const uint8_t CastResults[numCastOps][numCastOps] = {
1333 // T F F U S F F P I B -+
1334 // R Z S P P I I T P 2 N T |
1335 // U E E 2 2 2 2 R E I T C +- secondOp
1336 // N X X U S F F N X N 2 V |
1337 // C T T I I P P C T T P T -+
1338 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1339 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1340 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001341 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1342 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001343 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1344 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1345 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1346 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1347 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1348 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1349 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1350 };
1351
1352 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1353 [secondOp-Instruction::CastOpsBegin];
1354 switch (ElimCase) {
1355 case 0:
1356 // categorically disallowed
1357 return 0;
1358 case 1:
1359 // allowed, use first cast's opcode
1360 return firstOp;
1361 case 2:
1362 // allowed, use second cast's opcode
1363 return secondOp;
1364 case 3:
1365 // no-op cast in second op implies firstOp as long as the DestTy
1366 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001367 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001368 return firstOp;
1369 return 0;
1370 case 4:
1371 // no-op cast in second op implies firstOp as long as the DestTy
1372 // is floating point
1373 if (DstTy->isFloatingPoint())
1374 return firstOp;
1375 return 0;
1376 case 5:
1377 // no-op cast in first op implies secondOp as long as the SrcTy
1378 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001379 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001380 return secondOp;
1381 return 0;
1382 case 6:
1383 // no-op cast in first op implies secondOp as long as the SrcTy
1384 // is a floating point
1385 if (SrcTy->isFloatingPoint())
1386 return secondOp;
1387 return 0;
1388 case 7: {
1389 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1390 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1391 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1392 if (MidSize >= PtrSize)
1393 return Instruction::BitCast;
1394 return 0;
1395 }
1396 case 8: {
1397 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1398 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1399 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1400 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1401 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1402 if (SrcSize == DstSize)
1403 return Instruction::BitCast;
1404 else if (SrcSize < DstSize)
1405 return firstOp;
1406 return secondOp;
1407 }
1408 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1409 return Instruction::ZExt;
1410 case 10:
1411 // fpext followed by ftrunc is allowed if the bit size returned to is
1412 // the same as the original, in which case its just a bitcast
1413 if (SrcTy == DstTy)
1414 return Instruction::BitCast;
1415 return 0; // If the types are not the same we can't eliminate it.
1416 case 11:
1417 // bitcast followed by ptrtoint is allowed as long as the bitcast
1418 // is a pointer to pointer cast.
1419 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1420 return secondOp;
1421 return 0;
1422 case 12:
1423 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1424 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1425 return firstOp;
1426 return 0;
1427 case 13: {
1428 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1429 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1430 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1431 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1432 if (SrcSize <= PtrSize && SrcSize == DstSize)
1433 return Instruction::BitCast;
1434 return 0;
1435 }
1436 case 99:
1437 // cast combination can't happen (error in input). This is for all cases
1438 // where the MidTy is not the same for the two cast instructions.
1439 assert(!"Invalid Cast Combination");
1440 return 0;
1441 default:
1442 assert(!"Error in CastResults table!!!");
1443 return 0;
1444 }
1445 return 0;
1446}
1447
1448CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1449 const std::string &Name, Instruction *InsertBefore) {
1450 // Construct and return the appropriate CastInst subclass
1451 switch (op) {
1452 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1453 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1454 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1455 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1456 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1457 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1458 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1459 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1460 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1461 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1462 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1463 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1464 default:
1465 assert(!"Invalid opcode provided");
1466 }
1467 return 0;
1468}
1469
1470CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1471 const std::string &Name, BasicBlock *InsertAtEnd) {
1472 // Construct and return the appropriate CastInst subclass
1473 switch (op) {
1474 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1475 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1476 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1477 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1478 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1479 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1480 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1481 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1482 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1483 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1484 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1485 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1486 default:
1487 assert(!"Invalid opcode provided");
1488 }
1489 return 0;
1490}
1491
Reid Spencer5c140882006-12-04 20:17:56 +00001492CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1493 const std::string &Name,
1494 Instruction *InsertBefore) {
1495 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1496 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1497 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1498}
1499
1500CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1501 const std::string &Name,
1502 BasicBlock *InsertAtEnd) {
1503 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1504 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1505 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1506}
1507
1508CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1509 const std::string &Name,
1510 Instruction *InsertBefore) {
1511 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1512 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1513 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1514}
1515
1516CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1517 const std::string &Name,
1518 BasicBlock *InsertAtEnd) {
1519 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1520 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1521 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1522}
1523
1524CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1525 const std::string &Name,
1526 Instruction *InsertBefore) {
1527 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1528 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1529 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1530}
1531
1532CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1533 const std::string &Name,
1534 BasicBlock *InsertAtEnd) {
1535 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1536 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1537 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1538}
1539
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001540CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1541 const std::string &Name,
1542 BasicBlock *InsertAtEnd) {
1543 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001544 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001545 "Invalid cast");
1546
Chris Lattner03c49532007-01-15 02:27:26 +00001547 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001548 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1549 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1550}
1551
1552/// @brief Create a BitCast or a PtrToInt cast instruction
1553CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1554 const std::string &Name,
1555 Instruction *InsertBefore) {
1556 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001557 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001558 "Invalid cast");
1559
Chris Lattner03c49532007-01-15 02:27:26 +00001560 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001561 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1562 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1563}
1564
Reid Spencer7e933472006-12-12 00:49:44 +00001565CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1566 bool isSigned, const std::string &Name,
1567 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001568 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001569 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1570 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1571 Instruction::CastOps opcode =
1572 (SrcBits == DstBits ? Instruction::BitCast :
1573 (SrcBits > DstBits ? Instruction::Trunc :
1574 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1575 return create(opcode, C, Ty, Name, InsertBefore);
1576}
1577
1578CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1579 bool isSigned, const std::string &Name,
1580 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001581 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001582 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1583 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1584 Instruction::CastOps opcode =
1585 (SrcBits == DstBits ? Instruction::BitCast :
1586 (SrcBits > DstBits ? Instruction::Trunc :
1587 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1588 return create(opcode, C, Ty, Name, InsertAtEnd);
1589}
1590
1591CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1592 const std::string &Name,
1593 Instruction *InsertBefore) {
1594 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1595 "Invalid cast");
1596 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1597 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1598 Instruction::CastOps opcode =
1599 (SrcBits == DstBits ? Instruction::BitCast :
1600 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1601 return create(opcode, C, Ty, Name, InsertBefore);
1602}
1603
1604CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1605 const std::string &Name,
1606 BasicBlock *InsertAtEnd) {
1607 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1608 "Invalid cast");
1609 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1610 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1611 Instruction::CastOps opcode =
1612 (SrcBits == DstBits ? Instruction::BitCast :
1613 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1614 return create(opcode, C, Ty, Name, InsertAtEnd);
1615}
1616
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001617// Provide a way to get a "cast" where the cast opcode is inferred from the
1618// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001619// logic in the castIsValid function below. This axiom should hold:
1620// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1621// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001622// casting opcode for the arguments passed to it.
1623Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001624CastInst::getCastOpcode(
1625 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001626 // Get the bit sizes, we'll need these
1627 const Type *SrcTy = Src->getType();
1628 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1629 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1630
1631 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001632 if (DestTy->isInteger()) { // Casting to integral
1633 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001634 if (DestBits < SrcBits)
1635 return Trunc; // int -> smaller int
1636 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001637 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001638 return SExt; // signed -> SEXT
1639 else
1640 return ZExt; // unsigned -> ZEXT
1641 } else {
1642 return BitCast; // Same size, No-op cast
1643 }
1644 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001645 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001646 return FPToSI; // FP -> sint
1647 else
1648 return FPToUI; // FP -> uint
1649 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1650 assert(DestBits == PTy->getBitWidth() &&
1651 "Casting packed to integer of different width");
1652 return BitCast; // Same size, no-op cast
1653 } else {
1654 assert(isa<PointerType>(SrcTy) &&
1655 "Casting from a value that is not first-class type");
1656 return PtrToInt; // ptr -> int
1657 }
1658 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001659 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001660 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001661 return SIToFP; // sint -> FP
1662 else
1663 return UIToFP; // uint -> FP
1664 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1665 if (DestBits < SrcBits) {
1666 return FPTrunc; // FP -> smaller FP
1667 } else if (DestBits > SrcBits) {
1668 return FPExt; // FP -> larger FP
1669 } else {
1670 return BitCast; // same size, no-op cast
1671 }
1672 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1673 assert(DestBits == PTy->getBitWidth() &&
1674 "Casting packed to floating point of different width");
1675 return BitCast; // same size, no-op cast
1676 } else {
1677 assert(0 && "Casting pointer or non-first class to float");
1678 }
1679 } else if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
1680 if (const PackedType *SrcPTy = dyn_cast<PackedType>(SrcTy)) {
1681 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1682 "Casting packed to packed of different widths");
1683 return BitCast; // packed -> packed
1684 } else if (DestPTy->getBitWidth() == SrcBits) {
1685 return BitCast; // float/int -> packed
1686 } else {
1687 assert(!"Illegal cast to packed (wrong type or size)");
1688 }
1689 } else if (isa<PointerType>(DestTy)) {
1690 if (isa<PointerType>(SrcTy)) {
1691 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001692 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001693 return IntToPtr; // int -> ptr
1694 } else {
1695 assert(!"Casting pointer to other than pointer or int");
1696 }
1697 } else {
1698 assert(!"Casting to type that is not first-class");
1699 }
1700
1701 // If we fall through to here we probably hit an assertion cast above
1702 // and assertions are not turned on. Anything we return is an error, so
1703 // BitCast is as good a choice as any.
1704 return BitCast;
1705}
1706
1707//===----------------------------------------------------------------------===//
1708// CastInst SubClass Constructors
1709//===----------------------------------------------------------------------===//
1710
1711/// Check that the construction parameters for a CastInst are correct. This
1712/// could be broken out into the separate constructors but it is useful to have
1713/// it in one place and to eliminate the redundant code for getting the sizes
1714/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001715bool
1716CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001717
1718 // Check for type sanity on the arguments
1719 const Type *SrcTy = S->getType();
1720 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1721 return false;
1722
1723 // Get the size of the types in bits, we'll need this later
1724 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1725 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1726
1727 // Switch on the opcode provided
1728 switch (op) {
1729 default: return false; // This is an input error
1730 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001731 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001732 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001733 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001734 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001735 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001736 case Instruction::FPTrunc:
1737 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1738 SrcBitSize > DstBitSize;
1739 case Instruction::FPExt:
1740 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1741 SrcBitSize < DstBitSize;
1742 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001743 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001744 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001745 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001746 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001747 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001748 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001749 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001750 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001751 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001752 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001753 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001754 case Instruction::BitCast:
1755 // BitCast implies a no-op cast of type only. No bits change.
1756 // However, you can't cast pointers to anything but pointers.
1757 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1758 return false;
1759
1760 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1761 // these cases, the cast is okay if the source and destination bit widths
1762 // are identical.
1763 return SrcBitSize == DstBitSize;
1764 }
1765}
1766
1767TruncInst::TruncInst(
1768 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1769) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001770 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001771}
1772
1773TruncInst::TruncInst(
1774 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1775) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001776 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001777}
1778
1779ZExtInst::ZExtInst(
1780 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1781) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001782 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001783}
1784
1785ZExtInst::ZExtInst(
1786 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1787) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001788 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001789}
1790SExtInst::SExtInst(
1791 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1792) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001793 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001794}
1795
Jeff Cohencc08c832006-12-02 02:22:01 +00001796SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001797 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1798) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001799 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001800}
1801
1802FPTruncInst::FPTruncInst(
1803 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1804) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001805 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001806}
1807
1808FPTruncInst::FPTruncInst(
1809 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1810) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001811 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001812}
1813
1814FPExtInst::FPExtInst(
1815 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1816) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001817 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001818}
1819
1820FPExtInst::FPExtInst(
1821 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1822) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001823 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001824}
1825
1826UIToFPInst::UIToFPInst(
1827 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1828) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001829 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001830}
1831
1832UIToFPInst::UIToFPInst(
1833 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1834) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001835 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001836}
1837
1838SIToFPInst::SIToFPInst(
1839 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1840) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001841 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001842}
1843
1844SIToFPInst::SIToFPInst(
1845 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1846) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001847 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001848}
1849
1850FPToUIInst::FPToUIInst(
1851 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1852) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001853 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001854}
1855
1856FPToUIInst::FPToUIInst(
1857 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1858) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001859 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001860}
1861
1862FPToSIInst::FPToSIInst(
1863 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1864) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001865 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001866}
1867
1868FPToSIInst::FPToSIInst(
1869 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1870) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001871 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001872}
1873
1874PtrToIntInst::PtrToIntInst(
1875 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1876) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001877 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001878}
1879
1880PtrToIntInst::PtrToIntInst(
1881 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1882) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001883 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001884}
1885
1886IntToPtrInst::IntToPtrInst(
1887 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1888) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001889 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001890}
1891
1892IntToPtrInst::IntToPtrInst(
1893 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1894) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001895 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001896}
1897
1898BitCastInst::BitCastInst(
1899 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1900) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001901 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001902}
1903
1904BitCastInst::BitCastInst(
1905 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1906) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001907 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001908}
Chris Lattnerf16dc002006-09-17 19:29:56 +00001909
1910//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00001911// CmpInst Classes
1912//===----------------------------------------------------------------------===//
1913
1914CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1915 const std::string &Name, Instruction *InsertBefore)
Reid Spencer542964f2007-01-11 18:21:29 +00001916 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001917 Ops[0].init(LHS, this);
1918 Ops[1].init(RHS, this);
1919 SubclassData = predicate;
1920 if (op == Instruction::ICmp) {
1921 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1922 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1923 "Invalid ICmp predicate value");
1924 const Type* Op0Ty = getOperand(0)->getType();
1925 const Type* Op1Ty = getOperand(1)->getType();
1926 assert(Op0Ty == Op1Ty &&
1927 "Both operands to ICmp instruction are not of the same type!");
1928 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001929 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001930 "Invalid operand types for ICmp instruction");
1931 return;
1932 }
1933 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1934 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1935 "Invalid FCmp predicate value");
1936 const Type* Op0Ty = getOperand(0)->getType();
1937 const Type* Op1Ty = getOperand(1)->getType();
1938 assert(Op0Ty == Op1Ty &&
1939 "Both operands to FCmp instruction are not of the same type!");
1940 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001941 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001942 "Invalid operand types for FCmp instruction");
1943}
1944
1945CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1946 const std::string &Name, BasicBlock *InsertAtEnd)
Reid Spencer542964f2007-01-11 18:21:29 +00001947 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001948 Ops[0].init(LHS, this);
1949 Ops[1].init(RHS, this);
1950 SubclassData = predicate;
1951 if (op == Instruction::ICmp) {
1952 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1953 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1954 "Invalid ICmp predicate value");
1955
1956 const Type* Op0Ty = getOperand(0)->getType();
1957 const Type* Op1Ty = getOperand(1)->getType();
1958 assert(Op0Ty == Op1Ty &&
1959 "Both operands to ICmp instruction are not of the same type!");
1960 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001961 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001962 "Invalid operand types for ICmp instruction");
1963 return;
1964 }
1965 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1966 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1967 "Invalid FCmp predicate value");
1968 const Type* Op0Ty = getOperand(0)->getType();
1969 const Type* Op1Ty = getOperand(1)->getType();
1970 assert(Op0Ty == Op1Ty &&
1971 "Both operands to FCmp instruction are not of the same type!");
1972 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001973 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001974 "Invalid operand types for FCmp instruction");
1975}
1976
1977CmpInst *
1978CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
1979 const std::string &Name, Instruction *InsertBefore) {
1980 if (Op == Instruction::ICmp) {
1981 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
1982 InsertBefore);
1983 }
1984 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
1985 InsertBefore);
1986}
1987
1988CmpInst *
1989CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
1990 const std::string &Name, BasicBlock *InsertAtEnd) {
1991 if (Op == Instruction::ICmp) {
1992 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
1993 InsertAtEnd);
1994 }
1995 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
1996 InsertAtEnd);
1997}
1998
1999void CmpInst::swapOperands() {
2000 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2001 IC->swapOperands();
2002 else
2003 cast<FCmpInst>(this)->swapOperands();
2004}
2005
2006bool CmpInst::isCommutative() {
2007 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2008 return IC->isCommutative();
2009 return cast<FCmpInst>(this)->isCommutative();
2010}
2011
2012bool CmpInst::isEquality() {
2013 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2014 return IC->isEquality();
2015 return cast<FCmpInst>(this)->isEquality();
2016}
2017
2018
2019ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2020 switch (pred) {
2021 default:
2022 assert(!"Unknown icmp predicate!");
2023 case ICMP_EQ: return ICMP_NE;
2024 case ICMP_NE: return ICMP_EQ;
2025 case ICMP_UGT: return ICMP_ULE;
2026 case ICMP_ULT: return ICMP_UGE;
2027 case ICMP_UGE: return ICMP_ULT;
2028 case ICMP_ULE: return ICMP_UGT;
2029 case ICMP_SGT: return ICMP_SLE;
2030 case ICMP_SLT: return ICMP_SGE;
2031 case ICMP_SGE: return ICMP_SLT;
2032 case ICMP_SLE: return ICMP_SGT;
2033 }
2034}
2035
2036ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2037 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002038 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002039 case ICMP_EQ: case ICMP_NE:
2040 return pred;
2041 case ICMP_SGT: return ICMP_SLT;
2042 case ICMP_SLT: return ICMP_SGT;
2043 case ICMP_SGE: return ICMP_SLE;
2044 case ICMP_SLE: return ICMP_SGE;
2045 case ICMP_UGT: return ICMP_ULT;
2046 case ICMP_ULT: return ICMP_UGT;
2047 case ICMP_UGE: return ICMP_ULE;
2048 case ICMP_ULE: return ICMP_UGE;
2049 }
2050}
2051
Reid Spencer266e42b2006-12-23 06:05:41 +00002052ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2053 switch (pred) {
2054 default: assert(! "Unknown icmp predicate!");
2055 case ICMP_EQ: case ICMP_NE:
2056 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2057 return pred;
2058 case ICMP_UGT: return ICMP_SGT;
2059 case ICMP_ULT: return ICMP_SLT;
2060 case ICMP_UGE: return ICMP_SGE;
2061 case ICMP_ULE: return ICMP_SLE;
2062 }
2063}
2064
2065bool ICmpInst::isSignedPredicate(Predicate pred) {
2066 switch (pred) {
2067 default: assert(! "Unknown icmp predicate!");
2068 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2069 return true;
2070 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2071 case ICMP_UGE: case ICMP_ULE:
2072 return false;
2073 }
2074}
2075
Reid Spencerd9436b62006-11-20 01:22:35 +00002076FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2077 switch (pred) {
2078 default:
2079 assert(!"Unknown icmp predicate!");
2080 case FCMP_OEQ: return FCMP_UNE;
2081 case FCMP_ONE: return FCMP_UEQ;
2082 case FCMP_OGT: return FCMP_ULE;
2083 case FCMP_OLT: return FCMP_UGE;
2084 case FCMP_OGE: return FCMP_ULT;
2085 case FCMP_OLE: return FCMP_UGT;
2086 case FCMP_UEQ: return FCMP_ONE;
2087 case FCMP_UNE: return FCMP_OEQ;
2088 case FCMP_UGT: return FCMP_OLE;
2089 case FCMP_ULT: return FCMP_OGE;
2090 case FCMP_UGE: return FCMP_OLT;
2091 case FCMP_ULE: return FCMP_OGT;
2092 case FCMP_ORD: return FCMP_UNO;
2093 case FCMP_UNO: return FCMP_ORD;
2094 case FCMP_TRUE: return FCMP_FALSE;
2095 case FCMP_FALSE: return FCMP_TRUE;
2096 }
2097}
2098
2099FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2100 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002101 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002102 case FCMP_FALSE: case FCMP_TRUE:
2103 case FCMP_OEQ: case FCMP_ONE:
2104 case FCMP_UEQ: case FCMP_UNE:
2105 case FCMP_ORD: case FCMP_UNO:
2106 return pred;
2107 case FCMP_OGT: return FCMP_OLT;
2108 case FCMP_OLT: return FCMP_OGT;
2109 case FCMP_OGE: return FCMP_OLE;
2110 case FCMP_OLE: return FCMP_OGE;
2111 case FCMP_UGT: return FCMP_ULT;
2112 case FCMP_ULT: return FCMP_UGT;
2113 case FCMP_UGE: return FCMP_ULE;
2114 case FCMP_ULE: return FCMP_UGE;
2115 }
2116}
2117
Reid Spencer266e42b2006-12-23 06:05:41 +00002118bool CmpInst::isUnsigned(unsigned short predicate) {
2119 switch (predicate) {
2120 default: return false;
2121 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2122 case ICmpInst::ICMP_UGE: return true;
2123 }
2124}
2125
2126bool CmpInst::isSigned(unsigned short predicate){
2127 switch (predicate) {
2128 default: return false;
2129 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2130 case ICmpInst::ICMP_SGE: return true;
2131 }
2132}
2133
2134bool CmpInst::isOrdered(unsigned short predicate) {
2135 switch (predicate) {
2136 default: return false;
2137 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2138 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2139 case FCmpInst::FCMP_ORD: return true;
2140 }
2141}
2142
2143bool CmpInst::isUnordered(unsigned short predicate) {
2144 switch (predicate) {
2145 default: return false;
2146 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2147 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2148 case FCmpInst::FCMP_UNO: return true;
2149 }
2150}
2151
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002152//===----------------------------------------------------------------------===//
2153// SwitchInst Implementation
2154//===----------------------------------------------------------------------===//
2155
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002156void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002157 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002158 ReservedSpace = 2+NumCases*2;
2159 NumOperands = 2;
2160 OperandList = new Use[ReservedSpace];
2161
2162 OperandList[0].init(Value, this);
2163 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002164}
2165
Misha Brukmanb1c93172005-04-21 23:48:37 +00002166SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002167 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
2168 SI.getNumOperands()) {
2169 Use *OL = OperandList, *InOL = SI.OperandList;
2170 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2171 OL[i].init(InOL[i], this);
2172 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002173 }
2174}
2175
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002176SwitchInst::~SwitchInst() {
2177 delete [] OperandList;
2178}
2179
2180
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002181/// addCase - Add an entry to the switch instruction...
2182///
Chris Lattner47ac1872005-02-24 05:32:09 +00002183void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002184 unsigned OpNo = NumOperands;
2185 if (OpNo+2 > ReservedSpace)
2186 resizeOperands(0); // Get more space!
2187 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002188 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002189 NumOperands = OpNo+2;
2190 OperandList[OpNo].init(OnVal, this);
2191 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002192}
2193
2194/// removeCase - This method removes the specified successor from the switch
2195/// instruction. Note that this cannot be used to remove the default
2196/// destination (successor #0).
2197///
2198void SwitchInst::removeCase(unsigned idx) {
2199 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002200 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2201
2202 unsigned NumOps = getNumOperands();
2203 Use *OL = OperandList;
2204
2205 // Move everything after this operand down.
2206 //
2207 // FIXME: we could just swap with the end of the list, then erase. However,
2208 // client might not expect this to happen. The code as it is thrashes the
2209 // use/def lists, which is kinda lame.
2210 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2211 OL[i-2] = OL[i];
2212 OL[i-2+1] = OL[i+1];
2213 }
2214
2215 // Nuke the last value.
2216 OL[NumOps-2].set(0);
2217 OL[NumOps-2+1].set(0);
2218 NumOperands = NumOps-2;
2219}
2220
2221/// resizeOperands - resize operands - This adjusts the length of the operands
2222/// list according to the following behavior:
2223/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2224/// of operation. This grows the number of ops by 1.5 times.
2225/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2226/// 3. If NumOps == NumOperands, trim the reserved space.
2227///
2228void SwitchInst::resizeOperands(unsigned NumOps) {
2229 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002230 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002231 } else if (NumOps*2 > NumOperands) {
2232 // No resize needed.
2233 if (ReservedSpace >= NumOps) return;
2234 } else if (NumOps == NumOperands) {
2235 if (ReservedSpace == NumOps) return;
2236 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002237 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002238 }
2239
2240 ReservedSpace = NumOps;
2241 Use *NewOps = new Use[NumOps];
2242 Use *OldOps = OperandList;
2243 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2244 NewOps[i].init(OldOps[i], this);
2245 OldOps[i].set(0);
2246 }
2247 delete [] OldOps;
2248 OperandList = NewOps;
2249}
2250
2251
2252BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2253 return getSuccessor(idx);
2254}
2255unsigned SwitchInst::getNumSuccessorsV() const {
2256 return getNumSuccessors();
2257}
2258void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2259 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002260}
Chris Lattnerf22be932004-10-15 23:52:53 +00002261
2262
2263// Define these methods here so vtables don't get emitted into every translation
2264// unit that uses these classes.
2265
2266GetElementPtrInst *GetElementPtrInst::clone() const {
2267 return new GetElementPtrInst(*this);
2268}
2269
2270BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002271 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002272}
2273
Reid Spencerd9436b62006-11-20 01:22:35 +00002274CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002275 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002276}
2277
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002278MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2279AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2280FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2281LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2282StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2283CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2284CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2285CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2286CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2287CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2288CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2289CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2290CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2291CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2292CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2293CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2294CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2295CallInst *CallInst::clone() const { return new CallInst(*this); }
2296ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
2297SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2298VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2299
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002300ExtractElementInst *ExtractElementInst::clone() const {
2301 return new ExtractElementInst(*this);
2302}
2303InsertElementInst *InsertElementInst::clone() const {
2304 return new InsertElementInst(*this);
2305}
2306ShuffleVectorInst *ShuffleVectorInst::clone() const {
2307 return new ShuffleVectorInst(*this);
2308}
Chris Lattnerf22be932004-10-15 23:52:53 +00002309PHINode *PHINode::clone() const { return new PHINode(*this); }
2310ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2311BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2312SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2313InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2314UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002315UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}